C# asp.net mvc 如何将视图与控制器操作相关联?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9896950/
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-08-09 11:07:49  来源:igfitidea点击:

how does asp.net mvc relate a view to a controller action?

c#asp.net-mvc

提问by Elad Benda

I have opened a sample ASP.NET MVC project.

我打开了一个示例 ASP.NET MVC 项目。

In HomeControllerI have created a method (action) named MethodA

HomeController我创建了一个名为的方法(动作)MethodA

public ActionResult MethodA()
{
    return View();
}

I have right clicked on MethodAand created a new view called MethodA1

我右键单击MethodA并创建了一个名为的新视图MethodA1

Re-did it and created a new view called MethodA2.

重做并创建了一个名为 的新视图MethodA2

  1. How is this magical relationship done? I looked for the config to tell the compiler that views MethodAXare related to action MethodA, but found none.

  2. What view will the controller return when MethodAis called?

  1. 这种神奇的关系是如何完成的?我寻找配置来告诉编译器视图MethodAX与 action 相关MethodA,但没有找到。

  2. 控制器在MethodA被调用时会返回什么视图?

采纳答案by Darin Dimitrov

The convention is that if you don't specify a view name, the corresponding view will be the name of the action. So:

约定是,如果您不指定视图名称,则相应的视图将是操作的名称。所以:

public ActionResult MethodA()
{
    return View();
}

will render ~/Views/ControllerName/MethodA.cshtml.

将呈现~/Views/ControllerName/MethodA.cshtml.

But you could also specify a view name:

但是你也可以指定一个视图名称:

public ActionResult MethodA()
{
    return View("FooBar");
}

and now the ~/Views/ControllerName/FooBar.cshtmlview will be rendered.

现在~/Views/ControllerName/FooBar.cshtml将呈现视图。

Or you could even specify a fully qualified view name which is not inside the views folder of the current controller:

或者你甚至可以指定一个完全限定的视图名称,它不在当前控制器的视图文件夹中:

public ActionResult MethodA()
{
    return View("~/Views/Foo/Baz.cshtml");
}

Now obviously all this assumes Razor as view engine. If you are using WebForms, replace .cshtmlwith .aspxor .ascx(if you are working with partials).

现在显然所有这些都假设 Razor 作为视图引擎。如果您使用的是 WebForms,请替换.cshtml.aspx.ascx(如果您使用的是部分)。

For example if there is no view it will even tell you where and in what order is looking for views:

例如,如果没有视图,它甚至会告诉您在哪里以及以什么顺序查找视图:

enter image description here

在此处输入图片说明

Remember: ASP.NET MVC is all about convention over configuration.

请记住:ASP.NET MVC 是关于配置的约定。

回答by AlexC

The MVC framework use convention over configuration. The framework calls the ExecuteResult on the ViewResult object (created by the return View();). The framework by convention then looks in a number of locations to find a view

MVC 框架使用约定优于配置。框架调用 ViewResult 对象(由返回 View(); 创建)上的 ExecuteResult。按照惯例,框架然后在多个位置查找以找到视图

If you are using areas the framework will look in the following locations for a view.

如果您正在使用区域,框架将在以下位置查找视图。

  • /Areas//Views/ControllerName/ViewName.aspx
  • /Areas//Views/ControllerName/ViewName.ascx
  • /Areas//Views/Shared/ViewName.aspx
  • /Areas//Views/Shared/ViewName.ascx
  • /Areas//Views/ControllerName/ViewName.cshtml
  • /Areas//Views/ControllerName/ViewName.vbhtml
  • /Areas//Views/Shared/ViewName.cshtml
  • /Areas//Views/Shared/ViewName.vbhtml
  • /Areas//Views/ControllerName/ViewName.aspx
  • /Areas//Views/ControllerName/ViewName.ascx
  • /Areas//Views/Shared/ViewName.aspx
  • /Areas//Views/Shared/ViewName.ascx
  • /Areas//Views/ControllerName/ViewName.cshtml
  • /Areas//Views/ControllerName/ViewName.vbhtml
  • /Areas//Views/Shared/ViewName.cshtml
  • /Areas//Views/Shared/ViewName.vbhtml

Without areas (or if you are using areas and no view has been found) the framework will look at the following locations

没有区域(或者如果您正在使用区域并且没有找到视图),框架将查看以下位置

  • /Views/ControllerName/ViewName.aspx
  • /Views/ControllerName/ViewName.ascx
  • /Views/Shared/ViewName.aspx
  • /Views/Shared/ViewName.ascx
  • /Views/ControllerName/ViewName.cshtml
  • /Views/ControllerName/ViewName.vbhtml
  • /Views/Shared/ViewName.cshtml
  • /Views/Shared/ViewName.vbhtml
  • /Views/ControllerName/ViewName.aspx
  • /Views/ControllerName/ViewName.ascx
  • /Views/Shared/ViewName.aspx
  • /Views/Shared/ViewName.ascx
  • /Views/ControllerName/ViewName.cshtml
  • /Views/ControllerName/ViewName.vbhtml
  • /Views/Shared/ViewName.cshtml
  • /Views/Shared/ViewName.vbhtml

As soon as the Framework tests a location and finds a file, then the search stops, and the view that has been found is used to render the response to the client.

一旦框架测试了一个位置并找到了一个文件,搜索就会停止,并且已经找到的视图用于向客户端呈现响应。

There are a number of overriden versions of the View method. The most common one is to render a specific view, outside of the framework convention, by calling it by name. For example

View 方法有许多覆盖版本。最常见的方法是在框架约定之外通过按名称调用来呈现特定视图。例如

return View("~/Views/AnotherIndex.cshtml");

As an interesting footnote, the framework looks for legacy ASP, C# and VB Razor views (aspx, ascx, cshtml and vbhtml) even though you have a specific view engine.

作为一个有趣的脚注,即使您拥有特定的视图引擎,该框架也会查找旧的 ASP、C# 和 VB Razor 视图(aspx、ascx、cshtml 和 vbhtml)。

回答by Salim

I was looking for the same and I just made a couple of tests and figured out. It doesn't save anywhere. To understand how it works; just do these steps:

我一直在寻找相同的东西,我只是进行了几次测试并弄清楚了。它不会保存在任何地方。了解它是如何工作的;只需执行以下步骤:

In your controller, right click, Add View Then enter a different View Name and Ctrl F5 you will get Server error in application.

在您的控制器中,右键单击,添加视图然后输入不同的视图名称和 Ctrl F5 您将在应用程序中收到服务器错误。

For example if you right click , Add View in following Index action method and type "Index2" in View name, you will get the error.

例如,如果您右键单击 ,在以下索引操作方法中添加视图并在视图名称中键入“Index2”,您将收到错误消息。

public class TestController : Controller
{
    // GET: Test
    public ActionResult Index()
    {
        return View();
    }
}

So basically there is a 1-1 matching between action name and View name. And you cannot add view for the same method so there is no need to save in a config file.

所以基本上动作名称和视图名称之间存在 1-1 匹配。并且您不能为相同的方法添加视图,因此无需保存在配置文件中。

Now change the view file name in Visual Studio from Index2.cshtml to Index.cshtml then Ctrl+F5. You should see it is working.

现在将 Visual Studio 中的视图文件名从 Index2.cshtml 更改为 Index.cshtml,然后按 Ctrl+F5。你应该看到它正在工作。

回答by Shahaji

In MVC controller action is not bound to view. It uses delegate mechanism to pickup the view.

在 MVC 控制器中,动作没有绑定到视图。它使用委托机制来拾取视图。

Model Binding(Mapping)

模型绑定(映射)