Friday, 12 February 2016

How to Configure Routing in MVC:

0). Route must be registered in Application_Start event in Global.ascx.cs file.
1). Routing plays important role in MVC framework. Routing maps URL to physical file or class.
2). Route contains URL pattern and handler information. URL pattern starts after domain name.
3). Routes can be configured in RouteConfig class. Multiple custom routes can also be configured.

--------------------------------------------------------------------------------------
Below Example:-

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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


URL                                            Controller                     Action         Id
http://localhost/home                    Home                             Index         Null
http://localhost/home/index/786  Home                             Index         786

No comments:

Post a Comment