views:

34

answers:

3

We have an MVC app that injects our content into a HTML wrapper which supplied by the client.

There are multiple wrappers, such as fund.html, share.html, factsheet.html, and 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.

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. 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. 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.

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. Now, the Controller doesn't map directly to a wrapper file.

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. I only want wrappers for ViewResults.

So, this being the case, I was wondering how best to determine which wrapper to use for which ViewResult I'm executing. 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. 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.

A: 

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.

Hope this helps...

Brad C.
+1  A: 

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.
  • But also allow for the override of wrappers on a per-action basis, using an ActionFilter.

Barring additional requirements changes (multiple wrappers in multiple content areas within your Masterpage, for exampl), this should cover your needs, no?

Jomdom
A: 

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.

Malcolm Frexner