asp.net-mvc-3 - MVC 3 View 如何绑定(bind)到 Controller Action

标签 asp.net-mvc-3 razor view controller

这更像是对 MVC 3 工作原理的“架构理解”。我真的很想知道 MVC 如何知道为给定的 Controller 操作调用哪个 View 。我可以猜测,当我们使用 Visual Studio 为 Controller 操作创建 View 时,它会将这些绑定(bind)存储在项目的某种文件中,对吗?

提前致谢。 马塞洛米亚拉 里约热内卢/巴西

最佳答案

它是使用 "Convention over Configuration" 的想法完成的.

如果您使用没有 View 的随机操作,您可以看到它实际上是在尝试一堆不同的约定,寻找匹配项:

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

它在 Views/ControllerNameViews/Shared 中查找与操作名称匹配的 View 文件(.aspx、.ascx、.cshtml 或 .vbhtml 扩展名) > 文件夹。

The view 'SomeActionWithNoView' or its master was not found or no view engine supports the searched locations. The following locations were searched:

~/Views/Home/SomeActionWithNoView.aspx

~/Views/Home/SomeActionWithNoView.ascx

~/Views/Shared/SomeActionWithNoView.aspx

~/Views/Shared/SomeActionWithNoView.ascx

~/Views/Home/SomeActionWithNoView.cshtml

~/Views/Home/SomeActionWithNoView.vbhtml

~/Views/Shared/SomeActionWithNoView.cshtml

~/Views/Shared/SomeActionWithNoView.vbhtml


可以使用 /Views 根目录中的 _ViewStart.cshtml 设置默认 View 布局:

@{
    Layout = "~/Views/Shared/MyDefaultLayout.cshtml";
}

可以根据需要在各个 View 中删除或更改它。

关于asp.net-mvc-3 - MVC 3 View 如何绑定(bind)到 Controller Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13078819/

相关文章:

c# - 如何覆盖 MVC 中的@url.content?

.net - 更好地理解 Orchard 对 'Shape' 的想法

stored-procedures - 网络矩阵和存储过程

ios - 从主视图切换到其他 View

sql - ORA-00972 : identifier is too long creating a view

ios - 隐藏 subview 会在 View 上留下微弱的阴影

jquery - 在MVC3中的Ajax.Beginform中调用自定义确认对话框

asp.net-mvc-3 - 使用 Crystal Reports 运行 Visual Studio 2013 解决方案

javascript - 表格中的 ASP.NET MVC Razor 重复按钮在第一个按钮之后没有响应

c# - 无法读取配置部分 'system.web.webPages.razor',因为它缺少部分声明