asp.net-mvc ASP.NET MVC 如何链接视图和控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2030539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:01:23  来源:igfitidea点击:

How does ASP.NET MVC link views and controllers?

asp.net-mvc

提问by Kristian Damian

What code does Visual Studio add (and where is it put?) when you right-click the controller method to link to the view?

当您右键单击控制器方法以链接到视图时,Visual Studio 添加了什么代码(以及它放在哪里?)?

How can one do this (link the controller & view) without Visual Studio?

如果没有 Visual Studio,如何做到这一点(链接控制器和视图)?

回答by Alexander Taran

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.

这都是约定俗成的。您将视图放在每个控制器的 Views/ControllerName 文件夹中,这是框架要查找的默认位置。但这在任何情况下都不是必须的。

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.

Framework 假设您想要与操作名称同名的视图,并在 Views/Controller/ 文件夹中查找它。然后查看/共享。

But in your actions you can write

但在你的行动中你可以写

return View("ViewName");

Framework will look for a View named "ViewName" then in same folders.

框架将在相同的文件夹中查找名为“ViewName”的视图。

So default name for a view would be the name of action being executed. And that's a convention.

因此,视图的默认名称将是正在执行的操作的名称。这是一个约定。

回答by Rajeesh

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.

默认情况下,asp.net MVC 使用 FormViewEngine,它是 IViewEngine 的实现。IViewEngine 有两个名为“FindView”和“FindPartialView”的方法,它们实际上从“Views/Controller/”文件夹中定位视图文件。

Thanks,
Rajeesh

谢谢,
拉吉什

回答by DM.

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.

Visual Studio 将在 ~/Views/{YourControllerName} 下创建一个文件夹(如果它不存在)并将您的视图放在那里。如果在那里找不到它,它将在 ~/Views/Shared 文件夹中查找。如果您想手动创建视图,您需要将您的页面添加到这些文件夹之一,最好是 ~/Views/{YourControllerName} 文件夹。点击 NerdDinner 教程,看看它的实际效果。

http://nerddinnerbook.s3.amazonaws.com/Intro.htm

http://nerddinnerbook.s3.amazonaws.com/Intro.htm

回答by Neil T.

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\CodeTemplatesfolder.

Visual Studio 使用模板来创建默认视图。模板位于[Visual Studio 安装目录]\Common7\IDE\ItemTemplates[CSharp|VisualBasic]\Web\MVC\CodeTemplates文件夹中。

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:

如果您希望手动创建 MVC .ASPX 页面,您只需创建一个空白页面并提供具有以下属性的 Page 指令:

  • Language ("C#" or "VB")
  • MasterPageFile (default is ~/Views/Shared/Site.Master)
  • Inherits (for strongly-typed models, use ViewPage<ModelClassName>; otherwise ViewPage)
  • 语言(“C#”或“VB”)
  • MasterPageFile(默认为~/Views/Shared/Site.Master
  • 继承(对于强类型模型,使用ViewPage< ModelClassName>;否则使用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.

对于用户控件 (.ASCX),同样的规则适用,除了不使用 MasterPageFile 属性并且您从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.

PS 命名空间不在我的任何类名之前的原因是因为我在我的 web.config 部分声明了它们。