mvc 3 razor 视图引擎使用“默认”意见
我正在公司的一个新项目中使用 Razor,我已经使用它 2 天了,恕我直言,已经发现了一些奇怪的行为。
我的 web 应用程序的根目录中有一个家庭控制器,在一个区域(称为 Area1)中有一个家庭控制器。在这两个控制器中,我都有一个 Index 操作,因此我在 Views 根文件夹中获得了一个 Index 视图,在 Area1\Views 文件夹中获得了另一个 Index 视图。如果我删除区域内的索引视图,即 Area1\Views\Index.cshtml 并且我请求 Area1\Home\Index,我不会收到有关视图引擎未找到该操作的视图的错误,但“基" 索引视图位于 \Views\Index.cshtml 中并进行渲染。
有人知道这是一个错误还是故意为之?如果是这样,有什么办法可以禁用这个默认值吗?
i'm using Razor for a new project in my company, and i have been playing with it for 2 days and already found some weird behaviour imho.
I have a home controller in the root of my webapp and a home controller in an area , call it Area1. In both these controllers I have an Index action and therefore I got an Index view in the Views root folder, and another one in the Area1\Views folder. If I remove the index view within the area , so Area1\Views\Index.cshtml and I request Area1\Home\Index, I don't get an error about the view engine not finding the view for that action , but the "base" Index view is found in \Views\Index.cshtml and rendered.
Someone knows if this is a bug or it's done by purpose ? If so, there's any way to disable this default ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对不是一个错误。
此行为允许轻松重用视图模板,例如用于错误处理。
ASP.NET MVC 的默认行为是,如果您这样做...:
...它会搜索带有要在多个位置使用的操作名称的视图模板:控制器的默认命名约定文件夹,即,/Area1/Views/Home/,然后/Area1/Views/Shared/,然后Area1/Views/,然后/Views/Shared/,然后/Views/
如果没有找到与操作名称匹配的视图,则会抛出错误。
默认行为就这么多。要“自定义”此行为,您只需执行以下操作:
在控制器操作中,您可以指定返回时要使用的视图模板的名称。 EG:
或者更好的是,如果使用 T4MVC:
因此,我认为您不需要关闭默认行为才能执行(无论)您想要的操作。控制器的作用是控制向用户显示哪些视图。这是最好的做法。
然而,ASP.NET MVC 是非常可配置的,因此我认为有一些方法和手段可以关闭它。
如果您想这样做,祝您好运,但遵循默认设置并了解 ASP.NET MVC 的工作原理更有意义,特别是如果您是初学者。
注意:
上述内容适用于 ASP.NET MVC 1、2、3,并将继续如此。这是所有视图引擎的默认行为,包括 Razor 和 WebForm 视图。
PS:
如果您关心的是 url 在用户浏览器中的外观,您可以使用路由注册来配置 url。
Definitely NOT a bug.
This behaviour allows easy reuse of View templates, eg, for error handling.
The default behaviour for ASP.NET MVC is that if you do...:
...it searches for the view template with the name of the action to use in a number of places: the default naming convention folder for the controller, ie, /Area1/Views/Home/, then /Area1/Views/Shared/ then Area1/Views/ then /Views/Shared/ then /Views/
If it finds no such View matching the name of the action, then it throws an error.
So much for the default behaviour. To "customize" this behaviour, you just need to do the following:
In your controller actions, you can specify the name of the View template to use when you return. EG:
or better still, if using T4MVC:
As a result, I don't see that you need to switch off the default behaviour to be able to do (whatever) you want. Controllers are there to, uhmmmm, control what views are used to display to the user. That is the best practice.
However, ASP.NET MVC is very configurable, so there are ways and means, I presume, to switch this off.
If you want to do this, good luck to you, but it makes much more sense to follow the defaults and get to understand how ASP.NET MVC works, especially if you are a beginner.
NOTE:
The above applies to ASP.NET MVC 1, 2, 3 and will continue to do so. It is the default behaviour for all View engines, including Razor and WebForm Views.
PS:
And you can configure the urls using route registration if your concern is the look of the url in the user's browser.