简体   繁体   English

asp.net mvc如何将视图与控制器动作联系起来?

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

I have opened a sample ASP.NET MVC project. 我打开了一个示例ASP.NET MVC项目。

In HomeController I have created a method (action) named MethodA HomeController我创建了一个名为MethodA的方法(动作)

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

I have right clicked on MethodA and 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 MethodAX are related to action MethodA , but found none. 我找的配置告诉意见编译器MethodAX都与动作MethodA ,但没有发现。

  2. What view will the controller return when MethodA is called? 调用MethodA时控制器将返回什么视图?

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.cshtml view 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: 或者您甚至可以指定一个完全限定的视图名称,该名称不在当前控制器的views文件夹中:

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

Now obviously all this assumes Razor as view engine. 现在显然所有这些都假设Razor是视图引擎。 If you are using WebForms, replace .cshtml with .aspx or .ascx (if you are working with partials). 如果您使用的是WebForms,请将.cshtml替换为.aspx.ascx (如果您正在使用partials)。

For example if there is no view it will even tell you where and in what order is looking for views: 例如,如果没有视图,它甚至会告诉您查找视图的位置和顺序:

在此输入图像描述

Remember: ASP.NET MVC is all about convention over configuration. 请记住:ASP.NET MVC完全是关于约定优于配置。

The MVC framework use convention over configuration. MVC框架使用约定优于配置。 The framework calls the ExecuteResult on the ViewResult object (created by the return View();). 框架调用ViewResult对象上的ExecuteResult(由返回View();)创建。 The framework by convention then looks in a number of locations to find a view 按惯例,该框架然后在许多位置查找以查找视图

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.aspx
  • /Areas//Views/ControllerName/ViewName.ascx /Areas//Views/ControllerName/ViewName.ascx
  • /Areas//Views/Shared/ViewName.aspx /Areas//Views/Shared/ViewName.aspx
  • /Areas//Views/Shared/ViewName.ascx /Areas//Views/Shared/ViewName.ascx
  • /Areas//Views/ControllerName/ViewName.cshtml /Areas//Views/ControllerName/ViewName.cshtml
  • /Areas//Views/ControllerName/ViewName.vbhtml /Areas//Views/ControllerName/ViewName.vbhtml
  • /Areas//Views/Shared/ViewName.cshtml /Areas//Views/Shared/ViewName.cshtml
  • /Areas//Views/Shared/ViewName.vbhtml /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.aspx
  • /Views/ControllerName/ViewName.ascx /Views/ControllerName/ViewName.ascx
  • /Views/Shared/ViewName.aspx /Views/Shared/ViewName.aspx
  • /Views/Shared/ViewName.ascx /Views/Shared/ViewName.ascx
  • /Views/ControllerName/ViewName.cshtml /Views/ControllerName/ViewName.cshtml
  • /Views/ControllerName/ViewName.vbhtml /Views/ControllerName/ViewName.vbhtml
  • /Views/Shared/ViewName.cshtml /Views/Shared/ViewName.cshtml
  • /Views/Shared/ViewName.vbhtml /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. 只要Framework测试一个位置并找到一个文件,搜索就会停止,并且找到的视图将用于呈现对客户端的响应。

There are a number of overriden versions of the View method. View方法有许多覆盖版本。 The most common one is to render a specific view, outside of the framework convention, by calling it by name. 最常见的是通过名称调用框架约定之外的特定视图。 For example 例如

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)。

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. 所以动作名称和视图名称之间基本上有1-1匹配。 And you cannot add view for the same method so there is no need to save in a config file. 并且您无法为同一方法添加视图,因此无需保存在配置文件中。

Now change the view file name in Visual Studio from Index2.cshtml to Index.cshtml then Ctrl+F5. 现在将Visual Studio中的视图文件名从Index2.cshtml更改为Index.cshtml,然后按Ctrl + F5。 You should see it is working. 你应该看到它正在运作。

In MVC controller action is not bound to view. 在MVC控制器中,动作不受约束。 It uses delegate mechanism to pickup the view. 它使用委托机制来拾取视图。

Model Binding(Mapping) 模型绑定(映射)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.net MVC中没有控制器或操作的情况下在文件夹中显示视图 - Display View in folder without Controller or Action in ASP.net MVC ASP.NET MVC RemoteAttribute不会在控制器中触发操作方法 - ASP.NET MVC RemoteAttribute does not trigger action method in controller ASP.NET MVC如何将控制器操作指向其他视图? - ASP.NET MVC How do I point a controller action to a different view? ASP.NET MVC Razor:如何在控制器动作中呈现Razor Partial View的HTML - ASP.NET MVC Razor: How to render a Razor Partial View's HTML inside the controller action 如何在ASP.NET MVC中将数据从视图传递到控制器动作? - How to pass data to controller action from view in asp.net mvc? 如何将自定义模型对象从剃刀视图传递到ASP.NET MVC 5中的控制器操作 - How to pass custom model object from razor view to controller action in ASP.NET MVC 5 ASP.NET MVC 2-如何为单个控制器操作覆盖注释? - ASP.NET MVC 2 - How to override an annotation for a single controller action? 如何在 Asp.net MVC 中使用 Ajax 命中控制器动作? - How To Hit Controller Action Using Ajax In Asp.net MVC? 如何从 ASP.NET 5 MVC 6 控制器操作返回 Javascript - How to return Javascript from an ASP.NET 5 MVC 6 controller action 如何在ASP.net MVC 5中限制对控制器操作的访问 - How to restrict access of controller action in ASP.net MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM