简体   繁体   English

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

[英]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.

相关问题 如何在MVC4中将视图中的列表返回到控制器 - How can I return a list from view to controller in MVC4 我如何使用 datapost 将参数从 jqgrid 传递到 controller(使用 MVC4 和 asp.net) - how can i pass parameter from jqgrid to controller with datapost (using MVC4 and asp.net ) 如何从jquery-datatable中选择一行,从控制器中获取更多数据并在表下显示它们? (mvc4) - How can I select a row from a jquery-datatable, get more data from the controller and show them under the table? (mvc4) 我如何在MVC4中获得TempData对象识别 - How Can I Get TempData Object Recognize in MVC4 如何使用序列化在MVC4中发送数据? - How can I send data in MVC4 using Serialization? 我如何获取MVC4 Url.Action方法在控制器之前包含哈希 - How do I get MVC4 Url.Action method to include a hash before the controller 如何在同一控制器中获得MVC4 Web API以支持HTTP动词和“操作”方法? - How do I get MVC4 Web API to support HTTP Verbs and “Action” methods in the same controller? 如何使用MVC4 Web API控制器调用两个以上的Get方法 - How to give a call to more than two Get method using mvc4 web api controller 在MVC4控制器操作中,如何仅在传入模型时验证模型? - In an MVC4 controller action, how can I validate a model only when it's passed in? 我如何无法在MVC4中发送响应? - How can i not send a response in mvc4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM