Thursday, 26 December 2013

Controller and Action in ASP.NET MVC

In this article we are going to see the controller and action in asp.net mvc, create a new project with internet application of MVC 4 project.Url in the browser maps to the physical file for ASP.NET, but for ASP.NET MVC it is map to the controller and there action.

In MVC always the url is maps to the controller and the action present with in the controller, there is mapping of physical file done only for ignore route.The return type of the action is actionresult, string, Jsonresult etc . .

When you expand the Controllers folder and see the Home Controller, i.e is look like same as below

    public class HomeController : Controller
    {
        public string  Index()
        {
             return “this is sample test”;
        }
    }



In browser you can see that the default uri for launch the Index action in Home controller.How it is maps the default controller and action.

sample url :  http://localhost:4545/MvcSample

output of the above url is the this is sample test
This url is maps to the default home controller and the index action.Let we see how it is done , when you see the Global.asax in the application_start event there you can see the Register the Route Event .

 Global.asax:

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);   /* Reason for Route */
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }



In the above code you can see the RouteConfig.RegisterRoutes Which is the reason for map the default route for the application. Now expand the App_Start folder  and see the RouteConfig.cs

RouteConfig.cs

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 }
            );
        }
    }



In the above code you can see the line routes.MapRoute which is the reason for map the default route mapping for the application.

name : attribute specify that the Default.

url : attribute specify in what format the url will be passed first {} specify that any name can be passed with that type . first with Controller next with action then parameter.

defaults: attribute specify the value for the default , where you can see the controller name is mention as default is "Home" and the Action is "Index" and id is set as urlparameter in optional mode.

In this you have to change the values to set any other controller to be default or change the Action for default action to execute instead of Index. id is maps to the input parameter of the index action.

Now we see the first line , routes.IgnoreRoute which is used to Ignore from the normal processing of MVC.

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

{resource} means any name with extension .axd is exclude form the processing like MVC, so it is process like ASP.NET , why it is ignore where it is used ?

When we want to see the Tracing information we have to enable and see the tracing information in the page itself, but if we want it as separate file then it is save as "trace.axd". So when user give request like

http://localhost:4545/trace.axd

Instead of  processing like MVC, it is process like ASP.NET i.e maps the actual file present in the physical location for the extension .axd, to see the trace information.

output of trace.axd:




      From this article you can learn what is the usage of the controllers and the action and how the default action is mapped ,and what is the usage of IgnoreRoute.

No comments:

Post a Comment