I have opened a sample ASP.NET MVC project.
In HomeController I have created a method (action) named MethodA
public ActionResult MethodA()
{
return View();
}
I have right clicked on MethodA and created a new view called MethodA1
Re-did it and created a new view called MethodA2.
-
How is this magical relationship done? I looked for the config to tell the compiler that views
MethodAXare related to actionMethodA, but found none. -
What view will the controller return when
MethodAis called?
The convention is that if you don’t specify a view name, the corresponding view will be the name of the action. So:
will render
~/Views/ControllerName/MethodA.cshtml.But you could also specify a view name:
and now the
~/Views/ControllerName/FooBar.cshtmlview will be rendered.Or you could even specify a fully qualified view name which is not inside the views folder of the current controller:
Now obviously all this assumes Razor as view engine. If you are using WebForms, replace
.cshtmlwith.aspxor.ascx(if you are working with partials).For example if there is no view it will even tell you where and in what order is looking for views:
Remember: ASP.NET MVC is all about convention over configuration.