Currently I have a URL structure like when someone opens www.mysite.com/default.aspx?pid=50 then it searches for the pid 50 in the database pulls out the corresponding data against pid = 50 then displays the page according to the pulled data, and by looking
at URLs no one can ever get an idea of the contents of that page. I want that whenever user open an link like www.mysite.com/default.aspx?pid=50 then it should pull out the title entry against the pid = 50 and then transform it into lower letters and then
add underscores instead of space, which I will do by using this simple code
Dim lowerstring As String
Dim lowerstringwus As String
lowerstring = realtitle.tolower() 'realtitle is the title entry against the specified pid
lowerstringwus = lowerstring.Replace(" ", "_")
Then the URL should look like www.mysite.com/detail/<title>.aspx
Further when user enters the same URL as above then the same page should open, so that bookmarks may not be get wasted.
I am using ASP.NET 3.5 so the second link is not for me, and I already have read the first link but the problem I have is not discussed here this is just for a limited number of pages, there is nothing about pulling the title from the database and then make
it as the URL of the page.
rat170
Member
23 Points
78 Posts
URL Rewrite Problem
Oct 23, 2009 02:07 AM|LINK
Hi,
Currently I have a URL structure like when someone opens www.mysite.com/default.aspx?pid=50 then it searches for the pid 50 in the database pulls out the corresponding data against pid = 50 then displays the page according to the pulled data, and by looking at URLs no one can ever get an idea of the contents of that page. I want that whenever user open an link like www.mysite.com/default.aspx?pid=50 then it should pull out the title entry against the pid = 50 and then transform it into lower letters and then add underscores instead of space, which I will do by using this simple code
Dim lowerstring As String Dim lowerstringwus As String lowerstring = realtitle.tolower() 'realtitle is the title entry against the specified pid lowerstringwus = lowerstring.Replace(" ", "_")Then the URL should look like www.mysite.com/detail/<title>.aspx
Further when user enters the same URL as above then the same page should open, so that bookmarks may not be get wasted.
I read somewhere to use URLrewriter.net
<rewriter> <rewrite url="~/products/books.aspx" to="~/products.aspx?category=books" /> <rewrite url="~/products/CDs.aspx" to="~/products.aspx?category=CDs" /> <rewrite url="~/products/DVDs.aspx" to="~/products.aspx?category=DVDs" /> </rewriter>But I have lots and lots of pages, so it becomes useless.
Please Help.
url rewiting
atifsarfraz
Participant
1862 Points
293 Posts
Re: URL Rewrite Problem
Oct 23, 2009 02:29 AM|LINK
You might have to use HttpContext.RewritePath() to manually rewrite the URL path, and have asp.net continue processing
the request. Here is an article for ASP.NET 2.0
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
For ASP.NET 4.0 use Response.RedirectToRoute() discussed here
http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Atif Sarfraz
Please mark this post as answered if it helped you!
rat170
Member
23 Points
78 Posts
Re: URL Rewrite Problem
Oct 23, 2009 02:38 AM|LINK
I am using ASP.NET 3.5 so the second link is not for me, and I already have read the first link but the problem I have is not discussed here this is just for a limited number of pages, there is nothing about pulling the title from the database and then make it as the URL of the page.
Thanks for reply.
Please Help me further.
url rewiting
rat170
Member
23 Points
78 Posts
Re: URL Rewrite Problem
Oct 23, 2009 11:12 AM|LINK
Pleaseeeeeeeeeeee Help.............................
rat170
Member
23 Points
78 Posts
Re: URL Rewrite Problem
Oct 23, 2009 04:29 PM|LINK
At least give me some more related material to read.........
abhinay86
Member
70 Points
33 Posts
Re: URL Rewrite Problem
Oct 23, 2009 05:02 PM|LINK
As Atif said,
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
This worked for me dude... U dont need to write for every page. U can use this
<rewriter>
<rewrite url="~/products/(.+).aspx" to="~/products.aspx?category=$1" />
</rewriter>
They have a sample in there. download, run it n have a thorough look. U shud be able to get it
rat170
Member
23 Points
78 Posts
Re: URL Rewrite Problem
Oct 23, 2009 05:14 PM|LINK
What would I have to change in the code You mentioned, in order to get a value from database and make it the URL of the page.........?
For example if user enters ~/default.aspx?pid=43, a connection should be established with the database and then
title entry against pid=43 should be pulled and then this entry should appear in the url of that page.
further if the user enters the rewritten URL directly in the address bar, would it open the same page? Or will it give a 404 error?
Thank You very much.
abhinay86
Member
70 Points
33 Posts
Re: URL Rewrite Problem
Oct 23, 2009 05:58 PM|LINK
in the sample website given in
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
in web.config, first rename category to pid
<rewriter>
<rewrite url="~/products/(.+).aspx" to="~/products.aspx?pid=$1"/>
</rewriter>
So in default.aspx page,
if u want to give
Response.redirect(String.Format(Products.aspx?pid={0}",Textbox1.Text)) 'imagining that this textbox contains the querysring u want to send'
then u can directly give
response.redirect(String.Format("Products/{0}.aspx",Textbox1.text)
And product.aspx is the page which requests the querystring pid
--------------
U need to put a little extra effort to understand it
It took a little extra time for me to understand their code..
rat170
Member
23 Points
78 Posts
Re: URL Rewrite Problem
Oct 23, 2009 06:17 PM|LINK
Thank You very much for your reply, but I am real sorry I didn't got You.
I believe that response.redirect is for redirection, not for URL rewriting. It will only redirect to a page which already is there.
Please tell me that if I write write this code in my web.config file:
What will be URL rewritten when user enters ~/products.aspx?pid=7, would it be ~/products/7.aspx or am I getting it wrong?
Thank You very much.
abhinay86
Member
70 Points
33 Posts
Re: URL Rewrite Problem
Oct 23, 2009 06:27 PM|LINK
yes.
but not when user enters ~/products.aspx?pid=7 manually in the browser's addressbar..
its when u redirect from other page to ~/products.aspx?pid=7, then it will appear as ~/products/7.aspx