UrlMapping :: 忽略匹配块中的视图属性
给定 URI /admin/article/index,为什么此 url 映射不起作用?
"/admin/$controller/$action?/$id?"{
view = "/admin/index" // no dice, ignored
//action = "foo" // uncommented, this is picked up
}
我希望所有管理控制器默认使用管理视图(并且不必在每个控制器的每个操作中呈现视图)。对于“/account/$controller/...”和任何其他应使用通用视图的站点模块也是如此。
也许还有另一种方法可以实现这一目标,但假设 UrlMappings 是执行此操作的地方......
Given URI /admin/article/index, why would this url mapping not work?
"/admin/$controller/$action?/$id?"{
view = "/admin/index" // no dice, ignored
//action = "foo" // uncommented, this is picked up
}
I'd like for all admin controllers to use the admin view by default (and not have to render the view in each action of each controller). Same goes for "/account/$controller/..." and any other site module that should use a common view.
Perhaps there's another way to achieve this, but assumed that UrlMappings is the place to do it...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你正在尝试做一些与你所写的非常不同的事情。
您已经在基本 URL 映射中映射了
action
,并且视图是根据controller
自动选择的,因此您需要 定义不同映射那些没有控制器的视图,以及默认操作为foo
的项目的另一个映射。不过,控制器上的默认操作是index
,因此通常不需要在不指定控制器的情况下提供默认操作。我认为您总体上误解了 MVC 框架的工作原理。控制器不应该渲染任何内容,并且视图应该特定于控制器/操作。如果多个控制器正在渲染完全相同的视图,我愿意打赌要么控制器正在渲染 HTML,要么视图过于复杂。
您应该查看 SiteMesh 布局,它允许您创建默认模板结构,然后只需通过视图更改特定内容。
Looks like you are trying to do something very different than what you wrote.
You already have the
action
mapped in the base URL mapping, and the view is automatically selected based on thecontroller
, so you need to define different mappings for those views that don't have a controller, and yet another mapping for items with a default action offoo
. The default action on controllers isindex
, though, so there is usually no need to supply a default action without also specifying a controller.I think you are, in general, misunderstanding how an MVC framework works. The controller should not be rendering anything, and the views should be specific to the controller/action. If multiple controllers are rendering the exact same view, I'd be willing to bet that either the controller is rendering HTML or the view is overly complicated.
You should look into Layouts with SiteMesh, which allows you to create default template structures, then just have the specific content change through views.