ASP.NET MVC - pull and populate masterpage contents from database?

余生长醉 提交于 2020-01-13 06:05:20

问题


how can I pull masterpage content from the database and pass that into the masterpage so that my Views inherits from it? This is an example:

Clients to the website will have a unique code, lets call it "TargetCode", for example, ABC123, ABC456, etc... This unique code will be entered in the querystring, for example: mysite.com/ABC123.

Each of those "TargetCode" will have a different CSS, name, address, phone number, (common to all pages, so these will be placed in the master page) and page contents (around 2-3 pages, lets call thse pages Index, Products, and MoreInfo).

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

Thank you very much.


回答1:


Your master can also take in a MasterPage System.Web.Mvc.ViewMasterPage.MasterViewModel so I would have your controller call the model to get the resources you need and then bind the proper view based on your controller view calls.




回答2:


This is what I'm currently having and it 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()?



来源:https://stackoverflow.com/questions/1523073/asp-net-mvc-pull-and-populate-masterpage-contents-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!