ASP.NET MVC - 从数据库中拉取和填充母页内容? [英] ASP.NET MVC - pull and populate masterpage contents from database?

查看:151
本文介绍了ASP.NET MVC - 从数据库中拉取和填充母页内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数据库中提取masterpage内容并将其传递到母版页,以便我的视图继承它?这是一个示例:

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:

网站的客户端将有一个唯一的代码,称为TargetCode,例如ABC123,ABC456等...这个独特代码将被输入到查询字符串中,例如:mysite.com/ABC123。

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.

每个TargetCode都有不同的CSS,名称,地址,电话号码(所有页面通用,因此这些页面将放置在母版页中)和页面内容(约2-3页,允许调用索引,产品和更多信息)。

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

因此,当我访问网站地址mysite.com / ABC123时,首先它将查看数据库,检查代码是否存在,如果是,那么它会提取Masterpage信息(css,名称,地址,电话号码)并将其用于母版页。然后,我将拉取其他操作的页面内容(索引,产品和更多信息),所有这些页面将使用相同的母版页内容。

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.

谢谢非常多。

推荐答案

这是我目前拥有的,似乎工作,但我不知道是正确的做法:

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;
}

我应该使用OnActionExecuting()还是OnActionExecuted()?

Should I use OnActionExecuting() or OnActionExecuted()?

这篇关于ASP.NET MVC - 从数据库中拉取和填充母页内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆