Reputation: 627
When i do @Html.ActionLink("About us", "About", "Home")
, how does the application know that the controller file is in Controllers\HomeController.cs
? I see controller folders separated such as 'Views\Home', 'Views\Booking' etc. However, how does it know to look for Controllers\HomeController.cs
when the controller name specified was Home
? My code is perfectly working due to visual studio's generated files. However, i would like to know what is the theory behind it. Please explain, thank you.
Here is my route config,
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 }
);
}
here is my HomeController,
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
}
Upvotes: 0
Views: 95
Reputation: 838
Essentially, it's all .net mvc doing this. So when you feed it that ActionLink, it knows that "Home" is the Controller which houses a function named About.
When it's time to invoke the function to render something; the DefaultControllerFactory .net mvc uses will be start enough to figure out that the given controller is missing the word "Controller", so it will append it prior to invocation.
There's a really nice guide I've used in the past about how to override the DefaultControllerFactory: https://www.codeproject.com/Articles/599189/DefaultControllerFactory-in-ASP-NET-MVC
Which might give you a better sense of what's going on under the hood :)
Upvotes: 1