是否有.NET MVC像Ruby on Rails的任何应用程序控制器? [英] IS there any application controller for .net mvc like ruby on rails?

查看:81
本文介绍了是否有.NET MVC像Ruby on Rails的任何应用程序控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要每一个页面被请求的时间打数据库中的一些导航的东西。在Rails我只想设置在ApplicationController.rb一个实例变量,这将是提供给每个控制器/视图。

I have some navigation things that need to hit the database every time a page has been requested. In Rails I would just set an instance variable in the ApplicationController.rb and that would be available to every controller/view.

现在我看到我的所有控制器从固有的:在.NET MVC控制器。我可以打开控制器部分类,并添加一个动作过滤器,它会调用每次请求页面时?

Now I see that all my controllers inherent from : Controller in .NET MVC. Can I open a partial class for Controller and add an action filter that it would call every time a page is requested?

或者我应该创建的ApplicationController:控制器,并有我的所有其他控制器从继承

Or should I create an ApplicationController : Controller, and have all my other controllers inherit from that?

推荐答案

控制器是一个抽象的(不是部分)类,所以你要创建一个扩展控制器的基础控制器类,有你的控制器继承它。 // ASPNET:您可以在 HTTP找到源头code(对于目前RC1) 。codeplex.com / SourceControl / ListDownloadableCommits.aspx 。或者你可以简单地用适当的定制ActionFilterAttribute装点你的控制器,虽然我的preference将是自定义的基本控制器,除非过滤器需要采取为每个控制器不同的参数。

Controller is an abstract (not partial) class so you'll want to create a base controller class that extends Controller and have your controllers inherit from it. You can find the source code (for RC1 currently) at http://aspnet.codeplex.com/SourceControl/ListDownloadableCommits.aspx. Alternatively you can simply decorate your controllers with an appropriate custom ActionFilterAttribute, though my preference would be the custom base controller unless the filter needs to take parameters that differ for each controller.

这篇关于是否有.NET MVC像Ruby on Rails的任何应用程序控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆