URL Rewriting in ASP.Net


Steps for Implementing URL Rewriting in ASP.Net

           

1. Create a new page in ASP.Net  eg:  ‘Page1.aspx ‘

 

2. Draw a new button and label in the page.

 

3. Write “Welcome to Page 1” in the label and ‘Goto to Page 2’ in the button text.

 

4. Similarly create second page and use the same procedure as mention above.

 

5. Write the below code in Page 1 command button event

 

Response.redirect(“practice_in_C#.aspx?ptype=1”) where ptype is page type.

 

6. similarly

 

Response.redirect(“my_first_practice.aspx?ptype=2”) where ptype is page type.

 

 

7. We need to read the querystring data (ptype) and display the page accordingly.

 

8. Write the below code in Global.asax

 

void Application_BeginRequest(object sender, EventArgs e)

    {

        //url rewriting for SEO

        string strCurrentPath;

        int intPageType = 0;

        strCurrentPath = Request.Path.ToLower();

 

        if (Request.QueryString["ptype"] != null)

        {

            intPageType = Convert.ToInt16(Request.QueryString["ptype"]);

        }

        switch (intPageType)

        {

            case 1:

                {

                    Context.RewritePath(”page1.aspx”);

                    break;

                }

            case 2:

                {

                    Context.RewritePath(”page2.aspx”);

                    break;

                }

        }

 

    }

 

 

 

9. Now test the page, by running the file Page1.aspx and click on command button.

 

 

If you need to process the HTML extension file ( eg : ‘my_first_practice.html?ptype=1’ ) below are the steps you need to follow

 

  1. Goto IIS
  2. Open the proper box by right clicking on the virtual directory.
  3. Click on Configuration button
  4. Click on Add button from the Application Mapping Window.
  5. Write the path ‘C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll’  in the Executable textbox :
  6. Write ‘.html’ in the Extension textbox.
  7. Make sure the checkbox ‘Check that file exist’ is unchecked.
  8. Click on OK.

 

 

Its done… Enjoy !!!!!

maping1


by admin on April 1st, 2009 in ASP.Net

There are no comments.

Name*: Website: E-Mail*:
XHTML: You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>