Understanding Application Routes –This is what Microsoft
says
In my previous post I mentioned the URL is resolved via URL
routing module in ASP.Net MVC. I also specifically said that request must match
predefined URL pattern e.g. Customers/Add can be one such pattern which means I
want to add a customer or Customer/Show/Id=1 can be another such pattern which
means I want to show the details of the customer whose customer id is 1. Thus
we are moving away from our old way of depending upon files and tightly coupled
code behind it. Examine carefully don’t you think it’s like acting on resources
which compelled you to go in REST (Representational State Transfer) direction.
So just hold your breath although ASP.Net MVC allows you to use a pure REST
approach ASP.Net MVC is loosely REST oriented. Loosely REST oriented means it
does acknowledge concepts such as resource and action, but it leaves you free
to use your own syntax to express and implement resources and actions.
Route in ASP.Net MVC is a pattern matching string that
represents the absolute path of a URL. The route doesn’t include protocol, server
and port information. It can be constant string or combination of constant and
placeholder or only placeholder(s).
When you develop your ASP.Net MVC application you often come
across a File available in App_Start Folder by the name RouteConfig.cs. The
static RegisterRoutes method defines the pattern and the order in which ASP.Net
MVC runtime examines the URL to process the request. Routes are registered
through Global.asax file at application startup. If you go to Global.asax.cs
you will find a call to the static method RegisterRoutes of RouteConfig. routes.MapRoute
method adds the routes to the static collection defined by RouteTable.Routes.
Please note since the order of registering the route is
important register them from most specific to the least specific. Once the
rules are specified to check whether the URL is accepted the RouteHandler comes
into play. It implements IRouteHandler interface.
public interface IRouteHandler{ IHttpHandler
GetHttpHandler(RequestContext requestContext);}
The requestContext class encapsulates the HttpContext and route
specific data in RouteData object. Although we have devised a way to come out
from the dependency the request had on physical resources i.e. aspx in ASP.Net
Web Form application but what if we want our URLs to work for physical files
also. By default this setting is disabled in ASP.Net MVC Application even if
route exists for such URL but if we want to can enable it by setting routes.RouteExistingFiles
= true in RegisterRoutes method.
Additinally if you don’t want some URLs to be processes add
them via IgnoreRoute method. All that IgnoreRoute does is associate a StopRoutingHandler
route handler to the route built around the specified URL pattern.
No comments:
Post a Comment