ASP.NET MVC - 使用控制器相应地设置主视图
有没有什么简单的方法可以为特定控制器内的所有操作设置默认的 MasterView?
例如,如果我有 HomeController,我希望其中的所有操作都继承 Site.Master 作为默认值,但如果我在 AccountsController 内我希望所有操作都继承 Admin.Master 等等。
我设法做到这一点:
return View("viewName", "masterName", objectModel);
但是通过这样做,我必须在每次调用 View 方法时应用它。
我一直在寻找一些更简单的东西,比如在 Rails 上,我们可以声明:
class HomeController < ApplicationController
layout 'site'
def index
end
def create
...
end
class AccountsController < ApplicationController
layout 'admin'
def index
end
def create
...
end
这可能吗?
提前致谢
By any chance, is there any easy way to set a default MasterView for all the actions inside a specific controller?
For example If I have the HomeController I want all the actions inside it to inherit the Site.Master as default, but If I am inside AccountsController I want all the action to inherit the Admin.Master and so on..
I managed to do it with:
return View("viewName", "masterName", objectModel);
But by doing this I have to apply it every time I call the View method.
I was looking for something simpler like on rails where we can declare:
class HomeController < ApplicationController
layout 'site'
def index
end
def create
...
end
class AccountsController < ApplicationController
layout 'admin'
def index
end
def create
...
end
Is that possible?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以重写该 Controller 类中的 OnActionExecuting。
或者,如果您愿意,可以将其转换为 ActionFilterAttribute,您可以将其应用到控制器或操作级别,
然后依次使用,如下所示:
You could override OnActionExecuting in that Controller class.
Or if you like you can turn this into an ActionFilterAttribute you can apply on the controller or action level
which you then in turn use like so: