Tuesday, September 16, 2014

Routing MVC Asp.net

Routing enable you to use URL mapping incoming browser requests to particular MVC controller actions. When you create a new ASP.NET MVC application, the application is already configured to use ASP.NET Routing. The route table is configure RouteConfig.cs file   in App_Start folder in mvc application is given below
public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// The route with the pattern {resource}.axd/{*pathInfo} is included to prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id =UrlParameter.Optional }
            );


            routes.MapRoute(
              name: "AboutRoute",
              url: "{controller}/{action}/{id}/{dtTime}/{test}",
              defaults: new { controller = "Home", action = "Index", id =UrlParameter.Optional}
          );
        }
    }
In this file you can add multiple routes similar I have added the new “AboutRoute”.

Adding Route Constraints

    Route constraints can be used to further restrict the URLs that can be considered a match for a specific route. While we have so far examined whether routes match based upon URL structure and parameter names, Route Constraints allow us to add some additional specificity


routes.MapRoute("Category""Category/{CategoryId}",
                new { controller = "Category", action = "Details" },
               new { CategoryId = @"\d+" });//the regular expression \d+ matches one or more integers

The Global.asax file is a special file that contains event handlers for ASP.NET application lifecycle events. The route table is created during the Application Start event

RouteConfig.RegisterRoutes(RouteTable.Routes);

No comments:

Post a Comment

http://blogsiteslist.com