这更像是对 MVC 3 工作原理的“架构理解”。我真的很想知道 MVC 如何知道为给定的 Controller 操作调用哪个 View 。我可以猜测,当我们使用 Visual Studio 为 Controller 操作创建 View 时,它会将这些绑定(bind)存储在项目的某种文件中,对吗?
提前致谢。 马塞洛米亚拉 里约热内卢/巴西
最佳答案
它是使用 "Convention over Configuration" 的想法完成的.
如果您使用没有 View 的随机操作,您可以看到它实际上是在尝试一堆不同的约定,寻找匹配项:
public ActionResult SomeActionWithNoView()
{
return View();
}
它在 Views/ControllerName 或 Views/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/