簡體   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