[英]How can I get host in a Controller using MVC4
I would like to get a HOST name to apply an specific role for on host, because I have 2 websites hosted for the same application folder. 我想获得一个主机名,以在主机上应用特定角色,因为我为同一应用程序文件夹托管了两个网站。
I need get a HOST in my Controller to define this role. 我需要在Controller中获得一个HOST才能定义此角色。
Some like this: 像这样:
public override void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext); if (host == "mycustomhost.com") { ViewBag.Theme = "CustomTheme"; } else { ViewBag.Theme = "DefaultTheme"; } }
Have you tried something like this? 你尝试过这样的事情吗?
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.