Reputation: 1037
I inherited a solution which has alot of old code and and I am trying to clean it up.
I know that the Convention is that the View will have the same name as the Action Method.
However, When you right click in an Action Method and choose 'Add View' you can select a name for the View. Is there somewhere where this relation is mapped or kept.
I know that 'by convention' the view engine will search for a view with the name under:
~/Views/ControllerName ~/Views/Shared
but if you have given a view a name different than its Action Name how does MVC know to use that file, is there some data stored in the metadata of the method.
For example I have a
public class FooController : Controller {
public ActionResult Bar() {
return View();
}
}
but when I right-click and 'Add View...' I name the view Random.cshtml
How does this Controller Action method know to use this view. Where is this data stored.
Upvotes: 0
Views: 235
Reputation: 9304
The default view engine searches for a view with the same name of the action under ~/Views/[Controller]/[Action].cshtml
There is no metadata file or any sort of storage that contains the mappings.
You can write and inject your own view engine however, if you want to extend/change this behavior:
MVC4 Razor Custom View Locator
Upvotes: 0
Reputation: 1082
By default it will return the view with the same name as the Action, so in your case 'Bar'.
However, you can also specify a different view by name, like so:
return View("SomeViewOtherThanBar");
ETA: There is no mapping (that I know of) between a files/classes/etc. when you use the Visual Studio right-click helper to create views.
Upvotes: 1