Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-09-22
8
Medium Priority
?
244 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses

926 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