如何更改asp.net MVC 2的主题 [英] how to change the themes in asp.net mvc 2

查看:96
本文介绍了如何更改asp.net MVC 2的主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个选项,其中用户可以选择自己的主题,从下拉列表中的网站和主题适用于页[ATLEAST。

I would like to have an option wherein a user can choose his theme for the site from the dropdown list and the theme applies to that page [atleast].

我想这在ASP.NET MVC 2来完成,而不使用jQuery像框架。

I want this to be done in ASP.NET MVC 2 without using jquery like frameworks.

这怎么可能完成的。

我使用的是默认的Web表单视图引擎和DONOT想去用于此目的的自定义视图引擎。

I am using the default webforms viewengine and donot want to go for a custom viewengine for this purpose.

推荐答案

看来这是不支持开箱即用,但这里是我做了实现主题化:

It seems this is not supported out of the box, but here's what I did to implement theming:

首先,我加入了App_Themes文件夹到我的项目,并成立了几个主题

First, I Added the App_Themes folder to my project, and set up a couple of themes

然后我决定尝试模仿网络形式的资料提供者尽量靠近,并增加了配置文件属性来web.config中:

I then decided to try and mimic the Web-forms profile provider as close as possible, and added a profile-property to web.config:

<profile>
  <properties>
    <add name="ThemePreference" type="string" defaultValue="Blue" />
  </properties>
</profile>

所以,基本上我想要做的是能当主题改变加载不同的CSS的从相应的主题文件夹。我通过实现连接到UrlHelper类,这样我可以写一个辅助方法,这样做:

So, basically what I wanted to do was to be able to load the different css's from the appropriate theme-folder when the theme changed. I did this by implementing a helper method attached to the UrlHelper class so that I could write:

<link href="@Url.Theme("~/Content/Site.css")" rel="stylesheet" type="text/css" />

这样,就加载相应主题的site.css,并回落到〜/内容/如果没有找到文件的site.css。

This should then load the appropriate themed Site.css, and fall back to ~/Content/Site.css if no file was found.

助手是pretty简单:

The helper is pretty simple:

public static class UrlHelpers
{
    public static string Theme(this UrlHelper url, string u)
    {
        if (u.StartsWith("~")) u = u.TrimStart('~');
        SettingsProperty settingsProperty = ProfileBase.Properties["ThemePreference"];

        return url.Content("~/App_Themes/"+settingsProperty.DefaultValue + u);
    }
}

现在,在这个版本的code将其简单地得到默认值,所以你需要稍微调整了code的。但正如你所看到的,这不限于CSS的文件,但一切从的.master文件影像作品。

Now, in this version of the code it simply gets the default-value, so you'll need to tweak the code slightly. But as you can see, this is not limited to css-files, but works with everything from .master files to images.

更新 - 使用代替轮廓会议

public static class UrlHelpers
{
    public static string Theme(this UrlHelper url, string u)
    {
        if (u.StartsWith("~")) u = u.TrimStart('~');

        object currentThemeName = null;
        if (url.RequestContext.HttpContext.Session != null)
        {
            currentThemeName = url.RequestContext.HttpContext.Session["ThemePreference"];
        }
        return currentThemeName != null ? url.Content(String.Format("~/App_Themes/{0}{1}", currentThemeName, u)) : url.Content("~"+u);
    }
}

在返回线路在此方法中检验它是否找到了一个主题preference会话值,然后returnes所请求的内容相应的URL,因为它没有App_Theme文件$ P请求,否则它只是返回的内容$ PFIX。

The return-line in this method checks if it found a ThemePreference session-value, and then returnes the appropriate URL for the content requested, otherwise it simply returns the content as it was requested with no App_Theme prefix.

在您controlleraction为下拉的PostMethod,你会简单地做:

In your controlleraction for the DropDown postmethod, you'd simply do:

Session.Add("ThemePreference", whateverValueYouGotFromDropdown);

更新结束

随着一些调整和修复,这应该做的伎俩。

With some tweaking and fixing, this should do the trick.

希望它可以帮助一些,尽管它不是一个完整的演练:)

Hope it helps some, even though it's not a complete walkthrough :)

这篇关于如何更改asp.net MVC 2的主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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