如果 ViewEngines 集合中有多个引擎,MVC3 如何选择要使用的 ViewEngine?
我有一个内部开发的自定义视图引擎。在同一个项目中,我想对某些页面使用 Razor,对某些页面使用自定义引擎。 MVC框架如何选择使用哪个引擎?顺便说一句,我的自定义引擎不需要任何模板,它根据数据库中的元数据呈现页面。对于我的自定义引擎,我不想设置任何模板文件。 我期望的是应该有一种方法来驱动框架根据控制器名称和操作名称使用某些引擎。 MVC3 中存在这种灵活性吗?
I have a custom view engine developed internally. In the same project, I would like to use Razor for some pages and my custom engine for some pages. How does MVC framework choose which engine to use? BTW, my custom engine does not require any templates, it renders pages based on meta-data from database. For my custom engine I don't want to setup any template files.
What I am expecting is there should be a way to drive framework to use certain engine based on the controller name and action name.
Is this flexibility exists in MVC3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的视图引擎应实现 IViewEngine 接口。使用
ViewEngines.Engines.Add()
方法注册视图引擎后,MVC 框架将调用 FindView 和 FindPartialView 每当需要视图引擎来呈现视图时。多个视图引擎完全可以并行运行。如果您不希望在特定情况下使用视图引擎,请从
FindView
或FindPartialView<
return new ViewEngineResult(new string[0]);
/code> 和 MVC 将选择另一个视图引擎。如果您确实希望使用视图引擎,请返回有效的 ViewEngineResult 指向视图类(即实现 IView)想要渲染结果。useCache
参数有一些细节。如果您想了解更多信息,Louis DeJardin 在 TechEd 2011 上做了关于构建您自己的视图引擎的精彩演示。您可以在 Channel9 找到编写 ASP.NET MVC 视图引擎的视频。Your view engine should implement the IViewEngine interface. After you registered your view engine with the
ViewEngines.Engines.Add()
method, the MVC framework will call FindView and FindPartialView whenever it needs a view engine to render a view.It's absolutely possible for multiple view engines to operate side by side. If you don't want your view engine to be used in a specific situation you
return new ViewEngineResult(new string[0]);
fromFindView
orFindPartialView
and MVC will choose another view engine. If you do want your view engine to be used you return a valid ViewEngineResult pointing to the view class (that is implementing IView) that you want to Render the result.There are some specifics with the
useCache
parameter. If you want to know more, there was an excellent presentation on building your own view engine at TechEd 2011 by Louis DeJardin. You can find the video of Writing an ASP.NET MVC View Engine at Channel9.我认为最简单的方法是实现
IViewPageActivator
,http://bradwilson.typepad.com/blog/2010/10/service-location-pt11-view-page-activator.html 和http://msdn。 microsoft.com/en-us/library/system.web.mvc.iviewpageactivator(v=vs.98).aspx。我认为从
Create
方法返回 null 会使其稍后默认为默认的IViewPageActivator
。您将其注入DependencyResolver
,http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html。如果您使用 NInject 或 Unity 等依赖注入框架,可能会更容易使用。
I think the easiest way would be to implement a
IViewPageActivator
, http://bradwilson.typepad.com/blog/2010/10/service-location-pt11-view-page-activator.html and http://msdn.microsoft.com/en-us/library/system.web.mvc.iviewpageactivator(v=vs.98).aspx.I think that returning null from the
Create
method will make it later default to the defaultIViewPageActivator
. You inject it in theDependencyResolver
, http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html.It might be easier to use if you are using a dependency injection framework as NInject or Unity.