如何更改 ASP.NET MVC 2 中的主题
我想要一个选项,用户可以从下拉列表中选择网站的主题,并且该主题适用于该页面[至少]。
我希望在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎这不支持开箱即用,但这是我为实现主题所做的操作:
首先,我将 App_Themes 文件夹添加到我的项目中,并设置了几个主题
然后,我决定尝试尽可能模仿 Web 表单配置文件提供程序,并向 web.config 添加配置文件属性:
所以,基本上我所做的想要做的是能够在主题更改时从适当的主题文件夹加载不同的 css。我通过实现附加到 UrlHelper 类的帮助器方法来做到这一点,以便我可以编写:
然后,这应该加载适当的主题 Site.css,如果没有找到文件,则返回到 ~/Content/Site.css。
帮助器非常简单:
现在,在此版本的代码中,它只是获取默认值,因此您需要稍微调整代码。但正如您所看到的,这不仅限于 css 文件,还适用于从 .master 文件到图像的所有内容。
更新 - 使用会话而不是配置文件
此方法中的返回行检查是否找到 ThemePreference 会话值,然后返回所请求内容的适当 URL,否则它只是按原样返回内容请求时没有 App_Theme 前缀。
在 DropDown postmethod 的控制器操作中,您只需执行以下操作:
更新结束
经过一些调整和修复,这应该可以解决问题。
希望它能对一些人有所帮助,尽管这不是一个完整的演练:)
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 themes
I then decided to try and mimic the Web-forms profile provider as close as possible, and added a profile-property to web.config:
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:
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:
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
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:
Update ends
With some tweaking and fixing, this should do the trick.
Hope it helps some, even though it's not a complete walkthrough :)