Solved

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

Posted on 2014-09-22
8
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

739 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