Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 22, 2010 08:40 AM by rtpHarry
Member
51 Points
199 Posts
Jan 14, 2010 10:22 PM|LINK
I want to rewrite this simple url www.xyz.com/defalt.aspx?Id=1&Product=Acid. in format like
www.xyz.com/1/acid
Plz provide the full code to this.And more question that after rewriting the url how to get the QueryString.
plz help me soon.
All-Star
15625 Points
3146 Posts
Jan 15, 2010 02:30 AM|LINK
You hopefully will find help here:
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
364 Points
62 Posts
Jan 15, 2010 02:44 AM|LINK
Hi,
I did the following example.
I have a page named GoToDefault.aspx. It has only a button to redirect to Default.aspx.
This is the Code_Behind to GotoDefault.aspx:
protected void btnGoDefault_Click(object sender, EventArgs e) { Response.Redirect("http://localhost:1866/WebSlns/1/Acid"); }
Note that I use a pretty Url like you need.
Now I need to rewrite the Url, to go the appropiate page ( Default.aspx).
You need add global.asax to your site an put some code in the event Application_BeginRequest.
Here is the code to rewrite the URL:
void Application_BeginRequest(object sender, EventArgs e) { HttpContext context = HttpContext.Current; string path = context.Request.Path.ToLower(); int lastSlash = path.LastIndexOf('/'); if (!path.Substring(lastSlash + 1).Contains(".aspx")) { string id; string product; lastSlash = path.LastIndexOf('/'); product = path.Substring(lastSlash + 1); string partPath = path.Substring(0, lastSlash); lastSlash = partPath.LastIndexOf('/'); id = partPath.Substring(lastSlash + 1); context.RewritePath("~/Default.aspx?Id=" + id + "&Product=" + product, false); } }
Note that rewrite only works for pretty URLS, normal URLS ends in .aspx. This only works to example that you request, but can be extended.
Now in the Default.aspx you get parameters using QueryString in normal way.
This is the Code_Behind to my Default.aspx:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Response.Write(Request.QueryString["Id"] +"<br>"); Response.Write(Request.QueryString["Product"] + "<br>"); } }
I tested and this works fine.
I hope this help you.
Please, mark as answer if this solved your problem.
Jan 15, 2010 10:38 AM|LINK
Thanx for the response. i will try it.
but what about QueryString Values which i need.
Jan 16, 2010 07:55 AM|LINK
In this case how will i get querystring.
Jan 16, 2010 12:32 PM|LINK
The idea is not to change
www.xyz.com/defalt.aspx?Id=1&Product=Acid into
Is change www.xyz.com/1/acid into
www.xyz.com/defalt.aspx?Id=1&Product=Acid
And how I explained above you receive the parameters in normal way using QueryString. See the example.
You pass the parameters in the string of pretty URL:
www.xyz.com/2/JhonDoe
In this case, the parameters are: 2 and JhonDoe.
Contributor
4270 Points
735 Posts
Jan 16, 2010 12:54 PM|LINK
read this tutorial and download sample enjoy it
http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/
Jan 16, 2010 05:58 PM|LINK
Thanx for your response. and i unerstood what you want to say.
The probem is that these 2 parameters Id and Product are needed in product.aspx.
and these are defined in Global.aspx so how i will use these 2 parameters in product.aspx.
Plz Help
Jan 16, 2010 06:12 PM|LINK
u have to read full tutorial which i have send u
u have to do three things
1) change in web configfile
2) change in global file
3) and change in IIS which is mention in tutorial
Jan 18, 2010 05:50 AM|LINK
Plz reply...
Thanx in advance
Asp.NET Prof...
Member
51 Points
199 Posts
Url rewriting
Jan 14, 2010 10:22 PM|LINK
I want to rewrite this simple url www.xyz.com/defalt.aspx?Id=1&Product=Acid. in format like
www.xyz.com/1/acid
Plz provide the full code to this.And more question that after rewriting the url how to get the QueryString.
plz help me soon.
Mobile:9370350449
ankitcol@gmail.com
Mumbai
thuhue
All-Star
15625 Points
3146 Posts
Re: Url rewriting
Jan 15, 2010 02:30 AM|LINK
You hopefully will find help here:
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
ingecaam
Member
364 Points
62 Posts
Re: Url rewriting
Jan 15, 2010 02:44 AM|LINK
Hi,
I did the following example.
I have a page named GoToDefault.aspx. It has only a button to redirect to Default.aspx.
This is the Code_Behind to GotoDefault.aspx:
protected void btnGoDefault_Click(object sender, EventArgs e) { Response.Redirect("http://localhost:1866/WebSlns/1/Acid"); }Note that I use a pretty Url like you need.
Now I need to rewrite the Url, to go the appropiate page ( Default.aspx).
You need add global.asax to your site an put some code in the event Application_BeginRequest.
Here is the code to rewrite the URL:
void Application_BeginRequest(object sender, EventArgs e) { HttpContext context = HttpContext.Current; string path = context.Request.Path.ToLower(); int lastSlash = path.LastIndexOf('/'); if (!path.Substring(lastSlash + 1).Contains(".aspx")) { string id; string product; lastSlash = path.LastIndexOf('/'); product = path.Substring(lastSlash + 1); string partPath = path.Substring(0, lastSlash); lastSlash = partPath.LastIndexOf('/'); id = partPath.Substring(lastSlash + 1); context.RewritePath("~/Default.aspx?Id=" + id + "&Product=" + product, false); } }Note that rewrite only works for pretty URLS, normal URLS ends in .aspx. This only works to example that you request, but can be extended.
Now in the Default.aspx you get parameters using QueryString in normal way.
This is the Code_Behind to my Default.aspx:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Response.Write(Request.QueryString["Id"] +"<br>"); Response.Write(Request.QueryString["Product"] + "<br>"); } }I tested and this works fine.
I hope this help you.
Please, mark as answer if this solved your problem.
Asp.NET Prof...
Member
51 Points
199 Posts
Re: Url rewriting
Jan 15, 2010 10:38 AM|LINK
Thanx for the response. i will try it.
but what about QueryString Values which i need.
Mobile:9370350449
ankitcol@gmail.com
Mumbai
Asp.NET Prof...
Member
51 Points
199 Posts
Re: Url rewriting
Jan 16, 2010 07:55 AM|LINK
In this case how will i get querystring.
Mobile:9370350449
ankitcol@gmail.com
Mumbai
ingecaam
Member
364 Points
62 Posts
Re: Url rewriting
Jan 16, 2010 12:32 PM|LINK
Hi,
The idea is not to change
www.xyz.com/defalt.aspx?Id=1&Product=Acid into
www.xyz.com/1/acid
Is change www.xyz.com/1/acid into
www.xyz.com/defalt.aspx?Id=1&Product=Acid
And how I explained above you receive the parameters in normal way using QueryString. See the example.
You pass the parameters in the string of pretty URL:
www.xyz.com/2/JhonDoe
In this case, the parameters are: 2 and JhonDoe.
Aamir Hasan
Contributor
4270 Points
735 Posts
Re: Url rewriting
Jan 16, 2010 12:54 PM|LINK
read this tutorial and download sample enjoy it
http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/
Please Mark as Answered If its helpful you.
Asp.NET Prof...
Member
51 Points
199 Posts
Re: Url rewriting
Jan 16, 2010 05:58 PM|LINK
Thanx for your response. and i unerstood what you want to say.
The probem is that these 2 parameters Id and Product are needed in product.aspx.
and these are defined in Global.aspx so how i will use these 2 parameters in product.aspx.
Plz Help
Mobile:9370350449
ankitcol@gmail.com
Mumbai
Aamir Hasan
Contributor
4270 Points
735 Posts
Re: Url rewriting
Jan 16, 2010 06:12 PM|LINK
u have to read full tutorial which i have send u
u have to do three things
1) change in web configfile
2) change in global file
3) and change in IIS which is mention in tutorial
Please Mark as Answered If its helpful you.
Asp.NET Prof...
Member
51 Points
199 Posts
Re: Url rewriting
Jan 18, 2010 05:50 AM|LINK
Plz reply...
Thanx in advance
Mobile:9370350449
ankitcol@gmail.com
Mumbai