How to make skins in ASP.NET MVC

7

How would skins in MVC? where the client (user) could choose the skin (would change CSS, html, images, etc)?

OBS¹: Skin would be for all pages and each client can only have 1 single skin. But I would have about 10 different skins. These skins can be just a% of% different or a whole different site.

NOTE: If only the Controller can be the same for different skins, Ex: css will always return the same thing, only the view or _Layout may be different.

I imagine it in 3 ways, however as I do not have great experience in MVC I need FACTS on the advantages and disadvantages of each one, of course it may be that none of the 3 ways I imagine is the best

1 - Filters in the controller:

[VerificarSkin]
public ActionResult MostrarProduto()
{

2 - Changing the layout file by Controller or View

@{Layout = ViewBag.SkinAtual;}

3- Changing the View Folder [ Video explaining this solution a>]

public ActionResult MostrarProduto()
{
    var skinAtual = "skin12";
     return View("../Views/" + skinAtual + "/Home/index.cshtml");

Solutions:

1 - Advantages: Code Cleanup,

2 - Advantages: I take advantage of the same view for all the skins only mute the layout, however it can also be a disadvantage can not customize the view?

3 - Advantage: Can I change everything, view and layout, but would it give more work?

Scenario: Today I have 300 clients (users) where each one owns its website, these sites are shared from my tool, where it can choose the skins (which are from totally different color changes to layout)     

asked by anonymous 06.05.2016 / 19:07

1 answer

6
  

You do not have a way of talking which is the best, because it depends on what you will really need, I will explain better below. So I'll point out the strengths and weaknesses of these approaches.

First Form

The first way, as you yourself said, leaves the code much cleaner, and without redundancy, as it will create a filter to get the Skin and return to the user. I think it would look something like this:

 public class LayoutInjecterAttribute : ActionFilterAttribute
    {
        private readonly string _masterName;
        public LayoutInjecterAttribute(string masterName)
        {
            _masterName = masterName;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            var result = filterContext.Result as ViewResult;
            if (result != null)
            {
                result.MasterName = _masterName;
            }
        }
    }

[LayoutInjecter("_PublicLayout")]
public ActionResult Index()
{
    return View();
}

Source: How do I specify different Layouts in ASP.NET MVC 3 razor ViewStart file?

Of course, applying your needs. The disadvantage of this method is that you can not modify the Views with different information, that is, the Views will be the same, what will change will be the layout of the same. If this is not a problem, I find the best solution for you.

Second form

This form has the same disadvantages as the first one, you will only be able to change the Layout, but you still have one more disadvantage, you have to create ViewBag every Action , because the life cycle of ViewBag is not used in%% of different%. You can do in Actions or OnActionExecuted of the controller, but even so, you will have to do on each controler, or OnCreate , but I'm sure you know you should not "do" anything on it.

Third way

This is the way it will give you more work to implement, as you will have to replicate the code for every Skin you have, and as you said it will be about 10 ... I think you do not want this whole job. However, if you need to change something other than layout, in your View , this is the only way you can do what you want.

Also remember that you will need to get Skin in each Views , and this may not be feasible, depending on the form that Skin searches for.

Conclusion (Staff)

  

Before concluding, I should remember that when I say "do not change anything from View , I mean the code that is in that View in specific, but if it is something that can be shared for all, you will always have Shared View for this.

Of the forms presented, I believe that the first one would be the one indicated. In addition to not having to rewrite the code, you will be able to manipulate each _Layout differently if you wish. But if you need to change something in a Action or another, the first two ways will not suit you.

But depending on your context, nothing prevents you from using more than one way, using the third only where you need it.

    
06.05.2016 / 21:45