在 ASP.NET 中动态设置主题

发布于 2024-08-08 15:42:03 字数 440 浏览 1 评论 0原文

我有一个连接到不同域的应用程序,我没有复制和修改每个应用程序,而是在硬盘驱动器上使用相同的物理位置,但在 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 技术交流群。

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

发布评论

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

评论(1

不奢求什么 2024-08-15 15:42:03

实际上,它必须在Page_PreInit中设置,如果你试图在Page_Init中更改主题,它将不起作用。

最常见的解决方案是为所有页面使用父类。这是一次性的更改,并将逻辑放置在父类中。您可以继承自 ThemedPage,而不是从 Page 继承。当然,在继承自 Page 本身的 ThemedPage 类中,您可以重写 Page.OnPreInit 方法。

你要求“两行”,如果你消除混乱,它实际上是一条。这是 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# 的相同内容(仅类):

// 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 代码示例
更新:添加了 C# 代码示例

注意:主题必须存在否则您出现异常:在应用程序或全局主题目录中找不到主题“ThemeName”。。如果您想要默认主题或没有主题(如果主题不存在),请将其包裹在 try/catch 块中并使用 catch用于设置默认主题的块。

Actually, it must be set in Page_PreInit, it won't work if you try to change the theme in Page_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 class ThemedPage, which inherits from Page 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:

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

And instead of this:

Partial Class _Default
    Inherits System.Web.UI.Page

you now write this:

Partial Class _Default
    Inherits ThemedPage

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

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 a try/catch block and use the catch block for setting the default theme.

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