Solved

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

Posted on 2014-09-22
8
231 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Problem Solving Execerscise 5 40
ConsoleSql 1 14
Best Practice CSS floating images 2 15
Do we need servers??? 5 128
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Read about why website design really matters in today's demanding market.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmakerā€¦
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now