English 中文(简体)
ASP NET MVC2:动态主题
原标题:ASP .NET MVC 2: Dynamic themes

I would like to change dynamically the page theme in a MVC 2 Application. I found several solutions, but I want to use this method: in the Global.asax, change the current page theme:

   protected void Application_PreRequestHandlerExecute(object sender, System.EventArgs e)
    {
        // cast the current handler to a page object
        Page p = HttpContext.Current.Handler as page;

        if (p != null)
        {
            string strTheme = "Theme1";
            if (Convert.ToString(HttpContext.Current.Session["THEME"]) != string.Empty)
                strTheme = Convert.ToString(HttpContext.Current.Session["THEME"]);
            p.StyleSheetTheme = strTheme;
        }
    }

But this code always returns null in "p"... I ve also tried a similar code using the event PreRequestHandlerExecute in a HttpModule and the PreInit event of a page, but the code

HttpContext.Current.Handler as page

返回永远无效。

Can anyone help me? Thank you in advance.

问题回答

我没有在主题中使用 b,但我确实使用了 j子。 我处理该问题的方式是在我的主页上,我有逻辑,从一个共同的看法中得出目前的主题。 总页被严格分类为这种观点模式。 通用的图像特性由用户喜好和我的控制员继承的共同基底控制器的其他来源更新。 我在“行动”中这样做。 我检查结果是否是“意见”,然后从“意见”中得出结果。 财产归我共同的看法模式,确定财产。 主页使用该财产的价值来建立中央支助事务档案库。

<><><>>>>

public abstract class CommonViewModel
{
    public string Theme { get; set; }
    // ...
}

<><><><><>><>>>><>>>>>>

public abstract class BaseController : Controller
{
     public override void OnActionExecuted( ActionExecutedContext context )
     {
           if (context.Result is ViewResult)
           {
               var model = ((ViewResult)context.Result).ViewData.Model as CommonViewModel;
               if (model != null)
               {
                    var preferences = ...get from database for current user...
                    model.Theme = preferences.Theme;
               }
           }
    }
}

Master note it uses a custom HtmlHelper to generate the stylesheet link, you could do it by hand.

<%@ Master Language="C#"  Inherits="System.Web.Mvc.ViewMasterPage<...CommonViewModel>" >

<%: Html.Stylesheet( "themes/" + Model.Theme + ".css" ) %>

你们正在谈论的是标准蚊帐的工程,而不是像p.net MVC。 原因是(一般而言)p.net MVC没有使用标准网的网络控制模式,因此根本无法解释主题。

@tvanfosson有一些很好的建议。 仅仅记得,与多国公司一样,你对事情的控制要大得多,但这也意味着你必须做更多的工作,才能获得标准蚊帐规定的某些特征。 婚姻和家庭关系部使许多事情更加容易,但这不是其中之一。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签