3
Jul

How to Rewrite Url In Asp .Net

   Posted by: admin   in .Net Emperor

Hello Friends Please Read this Post very carefully and you will find out that urlRewriting is as simple as get ball from chlids hand.

Please Follow the steps

First you need to decide that from .aspx which extenstion you want to rewrite your page .html,.htm,.asp or with wildcard charactres.

After that you will add one class file in app_code folder name with as you want but whatever name is given by you please add that name in wb.config module section how i will tell you in same post.

I m adding file name “urlrewrite.vb”

Imports Microsoft.VisualBasic

Public Class urlrewrite
Implements IHttpModule

Public Sub Dispose() Implements System.Web.IHttpModule.Dispose

End Sub

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init

AddHandler context.BeginRequest, AddressOf OnBeginRequest

End Sub

Private Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)

Dim app As HttpApplication app = CType (sender, HttpApplication)

If app.Request.RawUrl.ToLower().Contains(”category.htm”)

Then

app.Context.RewritePath(”category.aspx”, “”, “msg=cat”)

ElseIf app.Request.RawUrl.ToLower().Contains(”product.html”)

Then

app.Context.RewritePath(”product.aspx”, “”, “msg=p”)

ElseIf

app.Request.RawUrl.ToLower().Contains(”test.aspx”)

Then

app.Context.RewritePath(”test.htm”, “”, “”)

End If

End Sub

End Class

After Complete this coding go to web.config add file name in module section

<system.web>
<httpModules>
<add name=”urlrewrite” type=”urlrewrite”/>
</httpModules>
</system.web>

After finish this all work enjoy url rewrting in .net application

Tags: , , ,

This entry was posted on Thursday, July 3rd, 2008 at 1:38 am and is filed under .Net Emperor. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One comment

Hiren
 1 

Thanks Arjun you really help me a lot i was finding this since from last few weeks thanks man

July 9th, 2008 at 1:27 am

One Trackback/Ping

  1. Virtual Files using HttpContext.RewritePath()    Jul 17 2008 / 11am:

    [...] How to Rewrite Url In Asp .Net [...]

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment