Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-09-22
8
Medium Priority
?
240 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

715 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