根据发布的域以不同方式呈现视图

发布于 2024-11-17 15:27:50 字数 264 浏览 5 评论 0原文

Mypage.cshtml 有一个页面,有一个域“xxx.com”和一个子域“test.xxx.com”,当我在子域“test.xxx.com”中发布我的网站时,页面的标题显示不同的内容。它会是这样的:

@if (--Request.IsSubDomain("test")--)
{ 
    <h1>Test page: Product</h1>
}
else
{ 
    <h1>Product</h1>
}

Mypage.cshtml have a page and have a domain "xxx.com" and a subdomain "test.xxx.com" that when I publish my site in the subdomain "test.xxx.com" the title of the page shows something different. It would be something like:

@if (--Request.IsSubDomain("test")--)
{ 
    <h1>Test page: Product</h1>
}
else
{ 
    <h1>Product</h1>
}

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

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

发布评论

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

评论(2

没有你我更好 2024-11-24 15:27:50

您不希望在您的视图中出现此登录信息。我个人会将其移至自定义属性中或直接将逻辑包含在控制器中。

顺便问一下,问题是什么?

编辑:

您可以使用Request.Url.Authority来确定域。

You would not want to have this login in your views. I personally would move this into a custom attribute or include the logic directly in your controllers.

By the way, what is the question?

Edit:

You could use Request.Url.Authority to determine the domain.

木格 2024-11-24 15:27:50

我通常不建议使用 ViewBag,但如果您想使用它来呈现标题,请从父控制器中子类化您的控制器,例如 GeneralController< /code> 并根据域设置一个 ViewBag.Title 属性。

一种替代方法是从包含类似逻辑的基本视图模型中对任何视图模型进行子类化。

public class GeneralController
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if(HttpContext != null)
            ViewBag.Title = GetRequestPath();        
    }

    private string GetRequestTitle()
    {
        if(HttpContext.Request.Path.Contains("test.xxx"))
            return "Test site";
    }
}

然后,任何控制器和随后渲染的视图都将能够使用此 ViewBag.Title 属性。事实上,MVC3 开箱即用地包含 _Layout.cshtml 作为其默认布局(或母版页),该布局已在头部包含以下行:

<title>@ViewBag.Title</title>

I don't normally recommend using ViewBag, but if you only want to use this to render a title, subclass your controllers from a parent controller say, GeneralController and set a ViewBag.Title property there based on domain.

One alternative to this is subclassing any view models from a base view model including similar logic.

public class GeneralController
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if(HttpContext != null)
            ViewBag.Title = GetRequestPath();        
    }

    private string GetRequestTitle()
    {
        if(HttpContext.Request.Path.Contains("test.xxx"))
            return "Test site";
    }
}

Then, any controller and subsequently rendered views will be able to use this ViewBag.Title property. In fact, out of the box, MVC3 includes _Layout.cshtml as its default layout (or Master Page) that already contains the following line in the head:

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