user2376512
user2376512

Reputation: 885

Reusable/Generic MVC controller for different front end

I am developing a MVC application for different Brand Partners, different Brands partners have different looks and feels, but all brands partners are doing the same functionality e.g. Login, Search Policy, and display policies details.

So How can I build a Generic/reusable controller for different brands partners ?

I know I can use controller factory, but I am not sure how to do it.

Upvotes: 0

Views: 495

Answers (1)

Pavel
Pavel

Reputation: 526

You could call different views in one Action. The implementation could look like this:

public class SomeController : Controller
{ 

    public ViewResult SomeAction(PartnerType partnerType) 
                                 // note: PartnerType could be anything you need
                                 //       enum, string, int
                                 //       also you may register your ModelBinder 
                                 //       and determinate value per request
    {
        // ... your logic 
        var viewName = "";
        switch(partnerType)
        {
            case: partner1
                viewName = "ViewForPartner1";
                break;
            case: partner2
                viewName = "ViewForPartner2";
                break;

            // other cases...

            default: 
                viewName = "DefaultView";
                break;
        }
        return View(viewName, model);
    }
}

Also there is another approach more complicated but without creating switch of view names each time, and will help to build you separate architecture for your app.

You need to register your own ViewEngine. Here is a good example how to do this. Then create your own pattern how to save views. I would do this way:

~/Views/%1/{1}/{0}

Where %1 is name of folder where views saved for your partner. The sctructure will look like this:

~/Views/FolderForPartner1/SomeController/SomeAction
~/Views/FolderForPartner2/SomeController/SomeAction
~/Views/FolderForPartner3/SomeController/SomeAction

Override "CreatePartialView", "CreateView" and "FileExists" to replace "%1" view folder name for current partner. You have access to ControllerContext, to pass parther Id(Name, Type). And each time your controllers call for view, the engine will take the exact view you need.

Upvotes: 4

Related Questions