Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ASP.NET MVC link views and controllers?

Tags:

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?

like image 599
Kristian Damian Avatar asked Jan 08 '10 20:01

Kristian Damian


People also ask

How does a controller find a view in MVC?

MVC by default will find a View that matches the name of the Controller Action itself (it actually searches both the Views and Shared folders to find a cooresponding View that matches). Additionally, Index is always the "default" Controller Action (and View).


1 Answers

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.

like image 130
Alexander Taran Avatar answered Sep 23 '22 06:09

Alexander Taran