动态设置在ASP.NET主题 [英] Dynamically setting themes in ASP.NET
问题描述
Ive得到了不同的域连接到一个应用程序,而不是复制和修改每个我使用的硬盘驱动器,但单独的应用程序池和网站在IIS上同一物理位置的应用程序。
Ive got an application that different domains are connected to, instead of copying and modifying each application i use same physical location on hard drive but separate application pools and websites on IIS.
基本上我想改变基于主机的一个主题。 IE浏览器。用户来到websome.com被websome的主题和用户说到jamessome.com被jamessome的主题。
Basically i want to change a theme based on hostname. ie. user comes to "websome.com" gets "websome" theme and user comes to "jamessome.com" gets "jamessome" theme.
我设置在web.config中页的属性,它适用于全球的主题,整个网站的主题。请问有什么办法可以修改基于域使用即时设定根据输入?这可能是可能的,但有什么缩小了和你的建议,以简化的解决方案几乎没有code做的事。按照我的理解,如果我编辑的web.config每次用户进来,将需要大量的时间,这不是优雅的......因此,任何ASP.NET大师在那里可以写code两线,使魔会怎样呢?
I set the theme in web.config "pages" attribute which applies the theme globally to whole website. Is there any way i can modify that setting on fly based on domain use entered from? It is probably possible but what are downsizes and what do you suggest to do with little code in order to simplify solution. As i understand if i edit web.config each time user comes in it will take lots of time which is not that elegant... So any ASP.NET gurus out there can write two lines of code so magic will happen ?
有在网站上的这些问题的几个解决方案,但是这将需要我code添加到这是不现实的网站上的每一页的Page_Init事件。
There are few solutions for these problem on the website but this will require me to add code to Page_Init event of every page on the site which is unrealistic.
推荐答案
其实,它必须以 Page_ preINIT
设置,它不会,如果你的工作试图改变主题 Page_Init
。
Actually, it must be set in Page_PreInit
, it won't work if you try to change the theme in Page_Init
.
最常见的解决方案是使用一个父类的所有网页。这是一次性只改变并将所述逻辑在父类。而不是从页面继承的
你再从,比如继承, ThemedPage
。类 ThemedPage
,从页
本身当然继承了里面,可以覆盖Page.On preINIT方法。
The most common solution is to use a parent class for all your pages. This is a one-time only change and places the logic in the parent class. Instead of inheriting from Page
you then inherit from, say, ThemedPage
. Inside the class ThemedPage
, which inherits from Page
itself of course, you can override the Page.OnPreInit method.
您提出要两条线,它实际上是一个,如果你删除了混乱。这是VB:
You asked for "two lines", it's actually one if you remove the clutter. This is VB:
Public Class ThemedPage
Inherits Page
Protected Overrides Sub OnPreInit(ByVal e As System.EventArgs)
Me.Theme = HttpContext.Current.Request.Url.Host.Replace(".com", "")
MyBase.OnPreInit(e)
End Sub
End Class
和,而不是这样的:
Partial Class _Default
Inherits System.Web.UI.Page
现在这样写:
Partial Class _Default
Inherits ThemedPage
这就是全部!一次性查找/替换就大功告成了。为了完整起见,这里是C#一样的(只有类):
That's all! A one-time search/replace and you're done. For completeness sake, here's the same (only the class) for C#:
// C# version
using System.Web;
using System.Web.UI;
public class ThemedPage : Page
{
protected override void OnPreInit(System.EventArgs e)
{
this.Theme = HttpContext.Current.Request.Url.Host.Replace(".com", "");
base.OnPreInit(e);
}
}
更新:添加VB code样品
更新:添加C#code样品的
Update: added VB code sample
Update: added C# code sample
注:主题的必须存在的,否则你会得到一个例外:主题'THEMENAME'不能在应用程序中找到或全局主题目录。
。如果你想有一个默认的主题或没有主题,如果主题不存在,将其套在一个尝试
/ 抓
块,并使用抓
块设置默认主题。的
Note: the theme must exist, otherwise you get an exception: Theme 'ThemeName' cannot be found in the application or global theme directories.
. If you want a default theme or no theme if the theme isn't there, wrap it around a try
/catch
block and use the catch
block for setting the default theme.
这篇关于动态设置在ASP.NET主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!