在 ASP.NET 中动态设置主题
我有一个连接到不同域的应用程序,我没有复制和修改每个应用程序,而是在硬盘驱动器上使用相同的物理位置,但在 IIS 上使用单独的应用程序池和网站。
基本上我想根据主机名更改主题。 IE。用户访问“websome.com”获取“websome”主题,用户访问“jamessome.com”获取“jamessome”主题。
我在 web.config“pages”属性中设置了主题,该属性将主题全局应用到整个网站。有什么方法可以根据输入的域使用动态修改该设置吗?这可能是可能的,但是什么是缩小规模以及您建议用很少的代码做什么以简化解决方案。据我所知,如果我每次用户进入时编辑 web.config 将花费大量时间,这不是那么优雅......所以任何 ASP.NET 专家都可以编写两行代码,这样神奇就会发生吗?
网站上针对这些问题的解决方案很少,但这需要我向网站上每个页面的 Page_Init 事件添加代码,这是不现实的。
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.
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.
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 ?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,它必须在
Page_PreInit
中设置,如果你试图在Page_Init
中更改主题,它将不起作用。最常见的解决方案是为所有页面使用父类。这是一次性的更改,并将逻辑放置在父类中。您可以继承自
ThemedPage
,而不是从Page
继承。当然,在继承自Page
本身的ThemedPage
类中,您可以重写 Page.OnPreInit 方法。你要求“两行”,如果你消除混乱,它实际上是一条。这是 VB:
而不是这个:
你现在写这个:
就这样!一次性搜索/替换就完成了。为了完整起见,以下是 C# 的相同内容(仅类):
更新:添加了 VB 代码示例
更新:添加了 C# 代码示例
注意:主题必须存在,否则您出现异常:
在应用程序或全局主题目录中找不到主题“ThemeName”。
。如果您想要默认主题或没有主题(如果主题不存在),请将其包裹在try
/catch
块中并使用catch
用于设置默认主题的块。Actually, it must be set in
Page_PreInit
, it won't work if you try to change the theme inPage_Init
.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 classThemedPage
, which inherits fromPage
itself of course, you can override the Page.OnPreInit method.You asked for "two lines", it's actually one if you remove the clutter. This is VB:
And instead of this:
you now write this:
That's all! A one-time search/replace and you're done. For completeness sake, here's the same (only the class) for C#:
Update: added VB code sample
Update: added C# code sample
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 atry
/catch
block and use thecatch
block for setting the default theme.