Wednesday, January 15, 2014

MVC 4 URL Routing Engine

Before MVC framework coming in to action there was  a direct relationship between URL and files on the server.
http://CJUdawatteSite.com/default.aspx       d:/webroot/default.aspx

with MVC4 there was no 1-1 relationship for URL coming and file in the server.it associate with controller class and action method.

routes.MapRoute("YourRoute", "contoller}/{action}", new {controller ="Home", action ="Index", id= UrlParameter.Optional});

Ex : http://mydomain.com/Home/Index 
      http://mydomain.com

routes.MapRoute("", "Public/{controller}/{action}",new { controller = "Home", action = "Index" });

Ex : http://mydomain.com/Public/Home/Index 
      http://mydomain.com/Public

routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
routes.MapRoute("ShopSchema2", "Shop/OldAction",new { controller = "Home", action = "Index" });
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index",id = "DefaultId" });
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index",id = UrlParameter.Optional });   
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index", id = "DefaultId" });
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index",id = UrlParameter.Optional });
  
Prioritizing Controllers by Namespaces

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index",id = UrlParameter.Optional},

new[] { "URLsAndRoutes.AdditionalControllers" });

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index",id = UrlParameter.Optional},

new[] { "URLsAndRoutes.AdditionalControllers", "UrlsAndRoutes.Controllers" });



Route myRoute = routes.MapRoute("AddContollerRoute","Home/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index",
id = UrlParameter.Optional },
new[] { "URLsAndRoutes.AdditionalControllers" });

myRoute.DataTokens["UseNamespaceFallback"] = false;  //To disable searching for controllers in other namespaces


Constraining Routes

Constraining a Route Using a Regular Expression


In this example, we have used a constraint with a regular expression that matches URLs only where
the value of the controller variable begins with the letter H.

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*"},
new[] { "URLsAndRoutes.Controllers"});  

URLs only when the controller variable begins with the letter H and the action variable is Index or About.

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*", action = "^Index$|^About$"},
new[] { "URLsAndRoutes.Controllers"});

Constraining a Route Using HTTP Methods

The format for specifying an HTTP method constraint is slightly odd. It does not matter what name
we give the property, as long as we assign it an instance of the HttpMethodConstraint class. In the listing,
we called our constraint property httpMethod to help distinguish it from the value-based constraints we
defined previously.
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*", action = "Index|About",
httpMethod = new HttpMethodConstraint("GET") },
new[] { "URLsAndRoutes.Controllers" });  // Ex : httpMethod = new HttpMethodConstraint("GET", "POST") 











No comments:

Post a Comment