如何更改 ASP.NET MVC 2 中的主题

发布于 2024-10-28 08:50:07 字数 170 浏览 0 评论 0原文

我想要一个选项,用户可以从下拉列表中选择网站的主题,并且该主题适用于该页面[至少]。

我希望在 ASP.NET MVC 2 中完成此操作,而不使用类似 jquery 的框架。

这怎么能实现呢。

我正在使用默认的 Webforms 视图引擎,并且不想为此目的而使用自定义视图引擎。

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

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

How can this be accomplished.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反话 2024-11-04 08:50:07

似乎这不支持开箱即用,但这是我为实现主题所做的操作:

首先,我将 App_Themes 文件夹添加到我的项目中,并设置了几个主题在此处输入图像描述

然后,我决定尝试尽可能模仿 Web 表单配置文件提供程序,并向 web.config 添加配置文件属性:

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

所以,基本上我所做的想要做的是能够在主题更改时从适当的主题文件夹加载不同的 css。我通过实现附加到 UrlHelper 类的帮助器方法来做到这一点,以便我可以编写:

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

然后,这应该加载适当的主题 Site.css,如果没有找到文件,则返回到 ~/Content/Site.css。

帮助器非常简单:

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

现在,在此版本的代码中,它只是获取默认值,因此您需要稍微调整代码。但正如您所看到的,这不仅限于 css 文件,还适用于从 .master 文件到图像的所有内容。

更新 - 使用会话而不是配置文件

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

此方法中的返回行检查是否找到 ThemePreference 会话值,然后返回所请求内容的适当 URL,否则它只是按原样返回内容请求时没有 App_Theme 前缀。

在 DropDown postmethod 的控制器操作中,您只需执行以下操作:

Session.Add("ThemePreference", whateverValueYouGotFromDropdown);

更新结束

经过一些调整和修复,这应该可以解决问题。

希望它能对一些人有所帮助,尽管这不是一个完整的演练:)

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

First, I Added the App_Themes folder to my project, and set up a couple of themesenter image description here

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>

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" />

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

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

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.

Update - Using Session instead of profile

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

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.

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

Session.Add("ThemePreference", whateverValueYouGotFromDropdown);

Update ends

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文