在 MVC3 中,如何全局覆盖 TempDataProvider?
我想更改 ASP.NET MVC3 应用程序中的 TempDataProvider...我知道我可以通过重写 CreateTempDataProvider
在每个控制器上执行此操作...但我想知道是否有办法对所有控制器在 1 个位置(“Global.asax?”)执行此操作。
我的原因是我的网站位于云服务器上......并且我想在某些情况下实现 Post-Redirect-Get 模式,但我不希望用户被发送到另一台服务器并且永远不会收到他的消息。
I'd like to change the TempDataProvider in an ASP.NET MVC3 application... I know I can do this on each controller by overriding CreateTempDataProvider
... but I was wondering if there is a way to do this in 1 spot ("Global.asax?") for all controllers.
My reason is that my site is on a cloud server... and I want to implement the Post-Redirect-Get pattern in some cases, but I don't want the user to be sent to another server and never get his message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你可以编写自己的ControllerFactory。然后,您可以在从
DefaultControllerFactory
基类检索控制器后,将TempDataProvider
设置为您的实现。在此处查看更多详细信息.这可能满足您的需要,但我个人更喜欢下一种方法:
我发现让所有控制器继承一些'base'控制器类是一个很好的做法。然后,可以在 1 个位置完成通用控制器逻辑(例如覆盖
CreateTempDataProvider
)。It seems you could write your own ControllerFactory. Here you could then, after retrieving the controller from the base
DefaultControllerFactory
class, set theTempDataProvider
to your implementation. See more details here.This probably does what you need, but personally I would prefer more then next approach:
I find it a good practice to have all your controllers inherit from some 'base' controller class. Common controller logic (like overriding
CreateTempDataProvider
can then be done in 1 place.