Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

[How Do I:] Implement URL Rewriting?

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[male speaker] Hello. Today I'd like to talk to you about URL rewriting in ASP.NET. The purpose of URL rewriting is to allow me to take a URL that's passed in and to change it into something else. I might want to do this for a couple of reasons. One of the main reasons is if I've rearranged my site and I've moved things, I don't want the bookmarks that users had previously to break. So I can use URL rewriting to take the old URL and map it to the new place. Another reason for using URL rewriting is to give the user user-friendly URLs— something that's easier to remember. So I'm going to show that in the example today. In order to show that I've created a simple website. It has a folder called Bikes, and in there is an ASP.NET page called Bike Info. The Bike Info page is fairly simple as you can see here. It has a form, a repeater control, and then I'm just going to show a link to a description— so href and a description. And I'm going to mock up all of the data. If I look at the code behind, you'll see that I have a page loaded int. In the page loaded int, I'm going to check to see if they passed in an item ID on their request. If not I'm going to use an ID of 0, which is going to return a page that lists the 2 different kinds of bikes I have—mountain bikes and road bikes. If they did pass an ID, I'll go to a page specific to that. And again, this is just mocked-up data that's going to show a little bit of text. So if I come down to my "get bike list" function, can see that I create a data table and add 2 columns to it—1 for link, 1 for description. If the ID passed in as one for mountain bikes, I'm going to add a couple of links. In this case the link goes to PageDoesntExist.aspx just because I don't need to show the other page. It's not important to this particular demonstration. And a little bit of text—"This bike is great for beginners" or "Here is a bike for serious mountain bikers." If the ID is 2, then I know that I have a road bike. And I am going to, again, link to the PageDoesntExist.aspx with some text for beginner road bikes or when you want to fly like the wind. And the final case—if there was no ID or if the ID is 0, then I'm going to have a page for mountain bikes that says, "See our selection of mountain bikes," and a page for road bikes with the text "See our selection of road bikes." If I run this page, you'll see that I get a link to BikeInfo.aspx. And then I can click on 1 of my 2 links here. If you'll notice in the status bar at the bottom of the browser, the URL goes to MountainBike.aspx or to RoadBike.aspx. If I try to click on one of these now, I'll get an error because I don't have a page named MountainBike.aspx or RoadBike.aspx. I'm going to use the URL rewriting so that it will handle that and redirect me back to the BikeInfo.aspx page with the correct ID associated with that type of bike. Another nice feature of this is that I would like to be able to use URL rewriting so if someone comes into the bikes, I come into the BikeInfo.aspx page instead of to my Default.aspx, which in this case is a page that says, "You should never see this page." In the Default.aspx there is a page holder so that the ASP.NET run time will pick up the request, see that it's for a URL that needs to be rewritten, and rewrite it. So I'm going to go ahead and close this, and I'm going to start rewriting. I'm going to write the code to rewrite the URLs. So the first thing that I need to do is add an ASP.NET folder. And I'll add my App_Code folder. Inside of the App_Code folder I want to add a new form, and I'll name my class UrlRewrite. So now I have my class URL rewrite, I want it to implement the IhttpModule interface. So I'm going to implement IhttpModule, and when I do that it will fill in the 2 methods for me— Dispose and and Init. I don't need to do anything in Dispose, but in Init I want to hook up the event in a page-processing life cycle where I want to rewrite the URL. At this one I have 3 events that I could hook up. I can hook up the begin request event, the authenticate request event, or the authorize request event. Which event I want to use really depends on my security model. If I'm not doing any authentication at all, any of the 3 events will work for me if I'm using forms authentication, but not Windows authentication. So in other words my ISS is set up for anonymous access, but in my web.config I set up forms authentication. I want to use the authorized request. If I use the begin request or the authenticate request, I will have rewritten the URL before the user has been authorized, and when I go into the authorization it will fail. If I'm using Windows authentication—in other words I've set up ISS to use the NTLM authentication— then I want to use the begin request or authenticate request. Since I'm just using anonymous authentication, I'll go ahead and in the Init hook up an event handler for the context begin request object and create a method called OnBeginRequest to handle that. So I'll create the private method. So inside of this event handler, I want to cast my center object as an HTTP application. Then I'm going to check the URL to see if it contains either MountainBike.aspx, RoadBike.aspx, or Default.aspx. So I'll start off and say if app.request.RawUrl.ToLower. I'm going to convert it to a lower-case string. And if it contains the string "/bikes/default.aspx," then I want to call app.Contex.RewritePath and give it the string BikeInfo.aspx. I'm going to use the override that has 3 methods. So the path info, I'll just pass in as a blank string. In this case I'm going to pass in a blank query string as well. I'm going to say ElseIf—instead of typing all this out, I will do an ElseIf—if it contains the string MountainBike.aspx, then I'm going redirect to BikeInfo.aspx with an ItemID=1. Or if it contains the string RoadBike.aspx, then I'm going to redirect with an ItemID=2. So all I've done here in my code is for Default.aspx, redirect back to BikeInfo with no query string. If it's a mountain bike, redirect with an item ID of 1; or a road bike, I redirect with an item ID of 2. Now I need to go into my HTTP—into my webconfig and hook up my HTTP module I just created. So I will add in HTTP modules—add, and I'm going to say name = "UrlRewrite," and the type is also going to equal UrlRewrite. Now I should have that all hooked up, and I should be able to run my page. So now we see with my BikeInfo.aspx, when I click on MountainBike.aspx it redirects me to the page MountainBike.aspx, and I see the text that was there. If I go back and click on the road bikes, you can see that I'm redirected to RoadBike.aspx, And even thought that page doesn't exist, under the covers it's redirecting me back to the BikeInfo.aspx with an ID of 2, and I see the information that I expected. And equally important if I'm an in-user and I can't remember how to get back to the page that lists all the different bikes, I can just go back, lop off the end part of the URL, hit enter, and now instead of going to that Default.aspx page and displaying that, I rewrite the URL, and I come up with the bikes page. Now I want to show you one problem that you might run into, and that's if you have a form that posts back to itself. So I've gone into my mountain bike page, and if I click on the submit button, you notice that I come back to the same page but my URL has changed. And now instead of saying MountainBike.aspx, it says BikeInfo.aspx with a question mark and ItemID=1. This isn't a real big problem, except it can be sort of disconcerting to the users where they think, "Wait—I was on MountainBike.aspx and now I'm on this different page." So there's a way to fix that. And to do that, we need to go back into our solution and add a new project. So in this case I'll add in a DLL project—Class Library—and I'm going to name this ActionlessForm. The reason I'm going to do that is this code is going to take and extend the HTML form control, such that it is going to remove the action property—or the action tag—from the webpage. The default behavior for the browser will then be to re-post back to the page that it came from instead of having the rewritten URL in there. So I'm going to rename my Class 1, and I'm going to call it Form. I need to go in and add a reference to the System.Web DLL so that I can get to and extend the HTML control—the HTML form control—sorry. So now I have my form1 and I want it to inherit System.Web.UI.HtmlControls.HtmlForm. And I need to override the Render Attributes method. So I'm going to go in there and use the writer parameter that's passed in, which is my HTML text writer, and write out the attribute for the name. And then I'm going to call the base class Attributes.Remove to remove the name that was passed in there. And I'm going to do the same thing with the method attribute. Then I want to write in an attribute—or render, I'm sorry— I'm going to render the attributes that I've set up so far in this HTML text writer, and then remove from the attributes collection on the base, the action attribute. And finally I'm going to say if—now I'm going to set it up so that if the base has an attribute, or in other words if the base is not nothing, then I want to write that attribute out into the resulting form. So I'll write out the client ID for that. Now go ahead and compile this and make sure I haven't made any syntax errors. And then I need to go back into my website and add a reference to that new DLL that I've created. I'll go over to the projects just to make it easier. When I do that it's going to create a bin folder for me and put that DLL in there. Then I need to go back into my BikeInfo.aspx page, and I need to register the DLL. So at the top of the page, I'll add in a register tag. I'll give it a tag prefix of MNF for My New Form. The name space is going to be ActionlessForm, and the assembly is also going to be ActionlessForm. Now all I need to do is come down here and change my form tag to use the MNF prefix. I tell it I want it to use that—the MNF or My New Form form— which will remove the action attribute from the form tag. So now if I run this, I can go into the mountain bike. And I notice if I View Source on this page that I have a form1, but it doesn't have an action. So now when I click on the Submit button, it will send it back to MountainBike.aspx. So hopefully you've seen how easy it is to implement URL rewriting so that you can help your users to come up with friendly URLs that are easy for them to remember and easy to navigate to, as well as avoiding errors when you've rearranged your site and old bookmarks don't work anymore.

Video Details

Duration: 22 minutes and 27 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 8
Posted by: neudesicasp on Aug 23, 2013

In this video Scott Golightly shows how to create an ASP.NET HttpModule to "rewrite" the URL when a request for a web page comes in. You may want to rewrite URLs to create friendly URLs or to direct an old URL to a new URL. We will look at the code needed to implement URL rewriting and also how to handle page post back events.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.