Click here to Skip to main content
15,042,336 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi friends,
I want to rewrite my URL,so i have written the following code.
protected void Application_BeginRequest(object sender, EventArgs e)
        {
   		HttpContext context = HttpContext.Current;
                string path = context.Request.Path.ToLower();
                if(path.Equals("/AndhraPradesh.aspx"))
                {
                    context.RewritePath("~/AP");
                }
        }


But still i am getting url as "http://localhost:22313/AndhraPradesh.aspx"

Please Help me and suggest me about url rewriting in global.asax

Thanking you
Posted
Updated 2-Nov-17 21:26pm

C#
string path = context.Request.Path.ToLower();
                if(path.Equals("/AndhraPradesh.aspx"))


path is in lowercase and you compare to a string with uppercases.
   
Comments
jaket-cp 6-Oct-15 6:46am
   
You are answering a 4 year old question.
That is nice and all, but you may get down voted, even though you have given a good solution.
It maybe a good idea to delete you answer.
See if these links help:

URL Rewriting with ASP.NET[^]

URL rewriting ASP.NET[^]

Also, the short URL doesn't contain any extensions so the IIS wouldn't be able to process it anyway and would throw a 404, to manage extensionless URLs you would need a third party ISAPI filter installed on the server side. However, if you are using IIS7 then an inbuilt tool called URLRewrite may be able to take care of that (not sure though). Plus, i would recommend using RegEx for path pattern matching which is how most URL rewriting engines work.

Alternatively you can try using path.contains , instead of path.Equals as this link suggests:

RewritePath[^]

Hope this helps...Cheers.
   
Comments
srinivas vadepally 22-Oct-11 6:48am
   
Hi gladiatron,
i am getting HTTP 404. I also tried with path.contains,getting same issue.
Is there any wrong with the code?
I.explore.code 22-Oct-11 8:44am
   
You are missing the key point here, rewritten URLs can only work if the new urls do actually exist on the server. I think you caught the opposite end of the stick with URL rewriting, the purpose of the URL rewriting is to make URLs memorable and hackable so that you can type in something like http://www.somsite.com/products/xbox/250GB but actually land on /products?prodname=xbox&cap=250gb. The former URL actually doesn't exist on the server but the latter one does and this is how URL rewriting works. I would also recommend using HTTPhandlers to the URL rewriting unless you only have to do it at the beginning of the application. So, if "AndhraPradesh.aspx" is a physical page on your server you cannot return "AP" to the user rather it works the other way round, when the user tries to access "AP" they would internally be forwarded to the physical file/page. hope this clears things up...
srinivas vadepally 24-Oct-11 0:36am
   
Thank you so much gladiatron,a small mistake made me mad for two days.
anyways thanks.
have a great time.
I.explore.code 24-Oct-11 1:10am
   
You are welcome! If my solution solved or helped solve your problem, can you please click on "Accept Solution" to accept it and rate it?? so people know that this has been solved. Many thanks.
Try this.

protected void Application_BeginRequest(object sender, EventArgs e)
        {
   		HttpContext context = HttpContext.Current;
                string path = context.Request.Path.ToLower();
                if(path.Equals("/andhrapradesh.aspx"))
                {
                    context.RewritePath("~/AP");
                }
        }
   
Comments
CHill60 3-Nov-17 15:07pm
   
You have added nothing new to this 6 year old thread! Stick to answering new questions where the OP still needs help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900