Solved

How to use short URL to redirect to correct web page?

Posted on 2014-09-22
8
233 Views
Last Modified: 2014-09-24
Using short URL method of ww.mydomainname/H/5555 which creates error so in Global.asax added code
which takes shorten URL and redirects to correct page with original parameter

 string cUrl = HttpContext.Current.Request.Url.PathAndQuery;

            if(cUrl.Contains("/H/"))
            {
                string cBeginDelim = "/H/";
                int nbpos = cUrl.IndexOf(cBeginDelim) + 1;
                string cParameter = cUrl.Substring(nbpos + cBeginDelim.Length - 1);
                string cPage = "~/downloadarea/correctpagerequired.aspx?Idparameter=" + cParameter;
                Server.ClearError();
                Response.Clear();
                Response.Redirect(cPage, true);

Open in new window


the problem is when it redirects to new page it retains the prefix of "/H/"

so intead of going to;
downloadarea/correctpagerequired.aspx?Idparameter=5555
it redirects to page;
/H/downloadarea/correctpagerequired.aspx?Idparameter=" + cParameter

the /H/ at beginning of the URL creates another error so it never redirects to the correct page?
0
Comment
Question by:stephenwilde
  • 5
  • 3
8 Comments
 

Author Comment

by:stephenwilde
ID: 40336665
Solved

Last line of code change false to true

Response.Redirect(cPage, false);

Open in new window


then it redirects

 if anybody has other better ways to redirect using / (forward slash) in URL please post.
0
 

Author Comment

by:stephenwilde
ID: 40337416
Baffling it works on Development machine changing URL manually to ;

http://localhost:49562/H/5555 

it redirect correctly using code in Application_Error in global.asax.cs

but on live web server;

www.mydomainname.com/H/5555 

comes up with Error 404 page!


Any help please would be gratefully appreciated.
0
 

Author Comment

by:stephenwilde
ID: 40337582
Looking into via Google searches - it may be something do with only asking for a no existent folder and no file name
so for that reason it may not enter Application_Error in global.asax.cs?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 19

Expert Comment

by:Manoj Patil
ID: 40338369
Hey try this

void Application_BeginRequest(object sender, EventArgs e)
{
 // Get the current path
 string CurrentURL_Path = Request.Path.ToLower();

 if (CurrentURL_Path.StartsWith("/H/"))
 {
   CurrentURL_Path = CurrentURL_Path.Trim("/");
   string NewsID = CurrentPath.Substring(CurrentPath.IndexOf("/"));
   HttpContext MyContext = HttpContext.Current;
   MyContext.RewritePath("/downloadarea/correctpagerequired.aspx?Idparameter=" +  ID);
 }
}

Open in new window

0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
ID: 40338373
0
 

Author Closing Comment

by:stephenwilde
ID: 40338513
Thank you for your posted code and link provided
0
 

Author Comment

by:stephenwilde
ID: 40341058
Although it redirects to the page in the code
 MyContext.RewritePath("/downloadarea/correctpagerequired.aspx?Idparameter=" +  ID);

the URL is still showing in the browser
/H/234

and the page referred to fails to load images it uses as the paths are all wrong

it there a way to change the path to the root and browser displayed url to;
/downloadarea/correctpagerequired.aspx?Idparameter=" +  ID
0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 40341468
Actually while loading the images you need to initiate it from the root by just putting "/" before path starts

Eg.

if your imagepath is images/logo.png then you need to make it like /images/logo.png
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question