25

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?

1
  • The answer you accepted, while very good and informative, doesn't answer your question so you should not mark it as accepted. Rajeesh's answer is perhaps a better one to accept.
    – Matthew
    Jul 29, 2014 at 23:04

4 Answers 4

34

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.

1
  • So simple, but super helpful. Thanks for being succinct. FYI this works with PartialView("ViewName") also and you can pass in your model on both of those methods also : View("ViewName", model)
    – raddevus
    Apr 24, 2018 at 17:12
5

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

0
1

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

1
  • Yes, i found the folder, and i saw those files, but i wanna know where are the link (if are any) between the aspx file and the controler method. Mi final goal is not depend of that VS wizard Jan 8, 2010 at 20:51
1

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:

  • Language ("C#" or "VB")
  • MasterPageFile (default is ~/Views/Shared/Site.Master)
  • Inherits (for strongly-typed models, use ViewPage<ModelClassName>; otherwise ViewPage)

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.