繁体   English   中英

如何使用MVC4在Controller中获得主机

[英]How can I get host in a Controller using MVC4

我想获得一个主机名,以在主机上应用特定角色,因为我为同一应用程序文件夹托管了两个网站。

我需要在Controller中获得一个HOST才能定义此角色。

像这样:

public override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);

    if (host == "mycustomhost.com")
    {
        ViewBag.Theme = "CustomTheme";
    }
    else
    {
        ViewBag.Theme = "DefaultTheme";
    }
}

你尝试过这样的事情吗?

public override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    base.Initialize(requestContext);

    var customHost = ConfigurationManager.AppSettings["CustomHost"];
    string theme = String.Empty;

    if (customHost.ToLower().Contains(requestContext.HttpContext.Request.Url.Host.ToLower()))
    {
        ViewBag.Theme = "CustomTheme";
    }
    else
    {
        ViewBag.Theme = "DefaultTheme";
    }
}
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    var hostname = requestContext.HttpContext.Request.Url.Host;

    // do something based on 'hostname' value
    // ....

    base.Initialize(requestContext);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM