Today, I would like to talk about the routing feature which is been introduced in the ASP.Net 4.0. here, in this case what happens basically, it will take an existing set of pages and re-write the urls in a frindly way. when i say friendly way it means user understandable, SEO optimized etc. so, the main enhancement which we see in asp.net 4.0 release is that routing module has been integrated by default for web forms. so, it has now become very easy to map the url request with the endpoints which we specify during configuring the same. so, what it exactly produce is come out with completely different kind of url scheme.
So, now see how does it work. well it uses the same pagerouteTable which ASP.Net MVC uses and in order for that to work, there is a handler PageRouteHandler which is automatically installed in ASP.Net 4. so, it’s job off course to dispatch requests onto pages. and the other interesting which is also included with this, is new function of RouteCollection is RouteCollection.MapPageRoute. basically, it defines the route. so, these things are pre-requisites for re-architecting the url scheme of the site.
now, in order to bring in routing i need to write the same in global.asax file. below, you can see the definition of MapPageRoute function.
but, here is catch i need to do one more configuration for running the same in IIS, i need to tell IIS to run managed Modules, otherwise, it would result me 404. here, it worked because i ran the same from my file system. so, we can instruct IIS from the web.config file by adding a simple entry as shown below.
so, the above instruction will make sure, that it will pickup the managed module installed for routing engine in ASP.Net 4.0. so, these are the basics. now, let’s focus on certain key points like ordering of route entries. This is very crucial because the 1st match is going to win.
Other important thing is that physical file will still take the precedence over route tables. so, it means using routing doesn’t change the existing functionality of ASP.Net means page based traversing, this will always remain as it is.
Another important thing is parameterized routes. it’s very much like query strings. so, rather than using query string, here we’ll be using segments of url, which is preferable as it is less prone to be hacked and understood by the hackers.
so, let’s see parameterized inputs. so, for this let me add one blank label on the page and the intention is just to grab the parameter value on the page load itself and the way i grab the value from the parameter is using the Page.RouteData to extract the data passed in the parameter like below
so, as you see above, this is 3 steps technique. but the crucial thing is route definition and fetching the same. however, you could also map the same to 404 url, in case there are certain values coming which doesn’t satisfy the scheme.
so, with this i would like to wrap this. Till then say tuned and happy coding.
797 total views, no views today