Sunday, July 18, 2010

ASP.net 4.0 Url Routing


In ASP.net 4.0 , New functionality included for URL Routing.

For Example : ( Application_Start)
RouteTable.Routes.MapPageRoute("Default","Home","~/Default.aspx");

When Browser request for http://<<your site>>/Home, It will route to page Default.aspx

And Page has some property that allow you to get route data, Page.RouteData

Even generic route is also possible.

RouteTable.Routes.MapPageRoute("Default","{Home}","~/{Home}.aspx");

Now When request made for Url : http://<<yoursite>/Test It automatically route to Test.aspx

One more thing true about ASP.net 4.0 routing is even though routing is done, there is still possible that direct request for “ASPX” pages.

To disable that

1. from UI (IIS 7.0 or later)
       - Go to inetmgr
       - In Feature View go to “Request Filtering”
         image  
       - Click Deny FileName Extension 
        image

2.  Using main web.config file of your application          
 <system.webServer>
     <modules runAllManagedModulesForAllRequests="true" />
        <security>
            <requestFiltering>
                <fileExtensions>
                    <add fileExtension=".aspx" allowed="false" />
                </fileExtensions>
            </requestFiltering>
        </security>
  </system.webServer>
    - Add Module tag with runAllManagedModulesForAllRequests = “true”
    - Add requestfiltering with fileextension=”.aspx” and “allowed=false”.


Let me know your comment on this.

No comments: