简体   繁体   English

如何根据正在执行的操作方法动态确定要使用哪个HTML“包装器”文件?

[英]How to dynamically determine which HTML 'wrapper' file to use based on the Action Method being executed?

We have an MVC app that injects our content into a HTML wrapper which supplied by the client. 我们有一个MVC应用,可将我们的内容注入到客户端提供的HTML包装中。

There are multiple wrappers, such as fund.html, share.html, factsheet.html, and home.html 有多个包装,例如fund.html,share.html,factsheet.html和home.html。

The wrapper is stored on our webserver. 包装器存储在我们的网络服务器上。 We need to load the wrapper into Site.Master and inject our content into the wrapper's content area, and then render the page. 我们需要将包装器加载到Site.Master中,然后将内容注入到包装器的内容区域中,然后呈现页面。

Basically, I need some advice on how to determine which wrapper to use. 基本上,我需要一些有关如何确定使用哪个包装器的建议。

At first, I was thinking that we could map a wrapper to a Controller. 起初,我以为我们可以将包装器映射到Controller。 So, if we're rendering a View on FundController we could use the wrapper Fund.html, and if it's ShareController, we could use Share.html. 因此,如果要在FundController上呈现视图,则可以使用包装器Fund.html,如果它是ShareController,则可以使用Share.html。 In BaseController, which is implemented by both FundController and ShareController I was getting the name of the Controller and using that to load the file, and all was good. 在由FundController和ShareController共同实现的BaseController中,我得到了Controller的名称,并使用它来加载文件,一切都很好。

But as requirements changed, it became apparent that this won't work because sometimes we need a controller to render different Views which use different wrappers. 但是随着需求的变化,显然这是行不通的,因为有时我们需要一个控制器来渲染使用不同包装器的不同视图。 So FundController might now have to render Fund.html & Factsheet.html. 因此,FundController现在可能必须呈现Fund.html和Factsheet.html。 Now, the Controller doesn't map directly to a wrapper file. 现在,Controller不再直接映射到包装文件。

I then thought that I could use the Action name instead of the Controller name to determine the wrapper I should use.. such as public ViewResult Fund() would correspond to Fund.html, and public ViewResult Factsheet() would correspond to Facthseet.html, but this is not ideal because the method that fires in BaseController will fire for all action methods that I execute, including those that return PartialViews and other Action Results, for which I don't want to load wrappers. 然后我以为可以使用Action名称而不是Controller名称来确定我应该使用的包装器。例如, public ViewResult Fund()对应于Fund.html, public ViewResult Factsheet()对应于Facthseet.html ,但这不是理想的方法,因为在BaseController中触发的方法将为我执行的所有操作方法(包括那些返回PartialViews和其他操作结果的方法)触发,而我不想为其加载包装器。 I only want wrappers for ViewResults. 我只想要ViewResults的包装器。

So, this being the case, I was wondering how best to determine which wrapper to use for which ViewResult I'm executing. 因此,是这种情况,我想知道如何最好地确定要对我正在执行的ViewResult使用哪个包装器。 One option is using a 'magic string' which, whenever a given Action Method is executing it sets a property in BaseController which can be used to determine the wrapper to use.. but I don't like magic strings. 一种选择是使用“魔术字符串”,每执行给定的操作方法,它都会在BaseController中设置一个属性,该属性可用于确定要使用的包装器。.但是我不喜欢魔术字符串。 I'd prefer to avoid a hacky approach. 我宁愿避免使用骇人听闻的方法。

I was also thinking of using an Action Filter which could do the work of loading the wrapper associated with whichever Action Method the filter is associated with. 我也在考虑使用动作过滤器,该动作过滤器可以完成与该过滤器关联的任何操作方法相关联的包装器的加载工作。 But before doing this, I'd like to get the opinion of other people who may have had to deal with an issue similar to this before. 但是在进行此操作之前,我想征询其他人的意见,这些人可能不得不处理以前与此类似的问题。 Or if anyone could advise on a better approach to take? 或者,如果有人可以建议采取更好的方法? What is the best way for me to go about doing this? 对我来说最好的方法是什么? Thanks. 谢谢。

It sounds like you've already spent a good amount of time thinking about the best way to handle this. 听起来您已经花了很多时间思考解决此问题的最佳方法。 Perhaps just combining your existing ideas: 也许只是结合您现有的想法:

  • By default, controllers & actions will retrieve their wrappers based on convention. 默认情况下,控制器和操作将根据约定检索其包装器。 FundController uses the Fund.html wrapper, and ShareController uses the Share.html wrapper. FundController使用Fund.html包装器,而ShareController使用Share.html包装器。
  • But also allow for the override of wrappers on a per-action basis, using an ActionFilter. 但也可以使用ActionFilter在每个动作的基础上重写包装器。

Barring additional requirements changes (multiple wrappers in multiple content areas within your Masterpage, for exampl), this should cover your needs, no? 除非有其他需求更改(例如,在母版页中多个内容区域中的多个包装),这应该可以满足您的需求,不是吗?

I think your Action Filter is one option, or you might want to Extend your very own ViewEngine that is responsible for deciding when to use the wrapper, and when not to. 我认为您的动作筛选器是一个选择,或者您可能想扩展自己的ViewEngine,后者负责决定何时使用包装器,何时不使用包装器。

Hope this helps... 希望这可以帮助...

I think it might be nice to mimic the rules of views. 我认为模仿观点规则可能会很好。 There is a rule where to look for views, but you can also state the viewname in the action. 有一个在哪里查找视图的规则,但是您也可以在操作中声明视图名称。

The nice thing would be that developers had the same set of rules. 令人高兴的是,开发人员拥有相同的规则集。

I dont know how you can implement this but I guess its possible. 我不知道如何实现此功能,但我想它是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM