Monday, November 24, 2014

Where does code execution begin in an ASP.NET MVC app?






http://stackoverflow.com/questions/21882726/where-does-code-execution-begin-in-an-asp-net-mvc-app

You should have a default route defined in the route config cs file. Add a matching controller action and view. Put a break point in the action method. That and the global.asax methods would be the first point user code runs (baring any filters added)


The Global.asax.cx file, where there is the start method ``, might be what you are looking for. That is the code which is run when the app starts.
protected void Application_Start()
{
    ...
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    ...
}
But looking at the url you have posted it might be the HomeController or DirectoryController file. Unfortunately I cannot tell from looking at your route.
A sample route register code is as below where we can see that
  1. The URL /{controller}/{action}/{id}
  2. The default for controller/action/id is Home/Index/optional
So if you run your web with the starting url as http://localhost:52763/, it is indeed will call http://localhost:52763/Home/Index
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 }
    );
}

Another example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace realEstate
{
    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 = "Rentals", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

No comments:

Post a Comment