ASP.NET MVC - pull and populate master page content from database?

how can I pull the content of the master page from the database and feed it to the master page so that my views inherit from it? This is an example:

Clients on the website will have a unique code, let's call it "TargetCode" like ABC123, ABC456, etc. This unique code will be entered in the query string, for example: mysite.com/ABC123.

Each of these "TargetCode" will have different CSS, name, address, phone number (common to all pages, so they will be placed on the main page) and page content (about 2-3 pages, lets call thse pages Index, Products and MoreInfo ).

So when I visit the website url mysite.com/ABC123, it will first look into the database, check if the code exists, if yes, then it pulls the homepage information (css, name, address, phone number) and use that for the home page. Then I will pull the page content (Index, Products and MoreInfo) for other Actions, all of these pages will use the same main page content of course.

Many thanks.

+2


source to share


2 answers


This is what I currently have and seems to work, but I'm not sure if this is the correct way to do it:

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    HomeRepository hr = new HomeRepository();
    var result = filterContext.Result as ViewResult;
    if (result == null)
        return;

    string TargetCode = string.Empty;
    Controller control = filterContext.Controller as Controller;
    System.Collections.Specialized.NameValueCollection query = filterContext.HttpContext.Request.QueryString;

    if (query.Count > 0 && query["TargetCode"] != null && query["TargetCode"].ToString() != "")
    {
        TargetCode = query["TargetCode"].ToString();
    }

    if (string.IsNullOrEmpty(TargetCode))
        if (control != null) control.HttpContext.Response.Redirect("./NoCode");

    if (!hr.CheckTargetCodeExists(TargetCode))
    {
        if (control != null) control.HttpContext.Response.Redirect("./InvalidCode");
    }
    var ThemeData = hr.GetMasterPageContent(TargetCode);
    result.ViewData["ThemeData"] = ThemeData;
}

      



Should I use OnActionExecuting () or OnActionExecuted ()?

+1


source


Your master can also take the MasterPage System.Web.Mvc.ViewMasterPage.MasterViewModel

, so I would ask your controller to call the model to get the resources you need, and then bind the proper view based on your controller calls.



+1


source







All Articles