How does ASP.NET MVC link views and controllers?

asp.net-mvc

Solution

It is all by convention. You place your views in the Views/ControllerName folder for every controller and that's the default location for framework to look for. But it is not a must in any way.

When in your controller you write

return View();

Framework assumes you want the view with the same name as action name and looks for it in Views/Controller/ folder. Then Views/Shared.

But in your actions you can write

return View("ViewName");

Framework will look for a View named "ViewName" then in same folders.

So default name for a view would be the name of action being executed. And that's a convention.

Problem

What code does Visual Studio add (and where is it put?) when you right-click the controller method to link to the view? How can one do this (link the controller & view) without Visual Studio?

Original source