Reputation: 1356
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?
Upvotes: 25
Views: 42511
Reputation: 4495
By default asp.net MVC uses FormViewEngine, which is an implementation of IViewEngine. IViewEngine has got two methods called "FindView" and "FindPartialView" which actually locates the view file from "Views/Controller/" folder.
Thanks,
Rajeesh
Upvotes: 5
Reputation: 6725
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.
Upvotes: 36
Reputation: 3320
Visual Studio uses templates to create the default views. The templates are located in the [Visual Studio Install Directory]\Common7\IDE\ItemTemplates[CSharp|VisualBasic]\Web\MVC\CodeTemplates folder.
If you wish to create an MVC .ASPX page manually, you need to simply create a blank page and provide a Page directive with the following attributes:
Example:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="ViewPage<ListCompanyManagerDetailsViewModel>" %>
For user controls (.ASCX), the same rules apply, except the MasterPageFile attribute is not used and you inherit from ViewUserControl.
Example:
<%@ Control Language="C#" Inherits="ViewUserControl<Contact>" %>
P.S. The reason that namespaces do not precede any of my class names is because I declared them in the section of my web.config.
Upvotes: 1
Reputation: 1847
Visual Studio will create a Folder (if it doesn't already exist) under ~/Views/{YourControllerName} and put your view in there. If it doesn't find it in there it will look in the ~/Views/Shared folder. If you want to manually create a view you need to add your page to one of those folders, preferably the ~/Views/{YourControllerName} folder. Hit up the NerdDinner tutorial to see this in action.
http://nerddinnerbook.s3.amazonaws.com/Intro.htm
Upvotes: 1