繁体   English   中英

如何将特定的子域请求重新路由到其他ASP.NET MVC控制器?

[英]How can I re-route a specific subdomain request to a different ASP.NET MVC controller?

我有一个ASP.NET MVC 4 Web应用程序。 它有一个默认主页,该主页处理对http://www.mydomain.comhttp://mydomain.com所有请求。 我还设置了一个MVC区域,可以通过www.mydomain.com/Foo/Hello/进行访问(其中“ Foo”是该区域的名称,“ Hello”是该区域的控制器)。

如果有人立即向foo.mydomain.com发出请求,它将把他们路由到默认的主页控制器。 我希望对foo子域根目录的所有请求(例如,未指定特定区域/控制器)自动重新路由到/foo/hello/控制器。

另外,我希望通过www子域对“ Foo”区域的任何请求都重定向到“ foo”子域。 即。 我希望所有对www.mydomain.com/Foo/Goodbye请求都自动重定向到foo.mydomain.com/Foo/Goodbye

我有,当然,看了看地段和大量的 其他 样本 / 问题 ,但他们都不来解决我的确切问题。

我不确定是否应该使用路由,路由约束,路由处理程序等解决此问题。

另外:该应用程序托管在Windows Azure Cloud Services上 ,因此我无法完全控制IIS设置。

在您的Web服务器中,站点的应用程序池必须具有集成管道模式,如下所示。

在此处输入图片说明

或者您可以通过“应用程序池”的“基本”设置找到它,如下所示。

在此处输入图片说明

然后我在示例MVC应用程序中添加了HttpModule

HTTP模块

public class MyModule1 : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
    }

    public void Dispose() { }

    void context_BeginRequest(object sender, EventArgs e)
    {
        if (!System.Web.HttpContext
                       .Current
                       .Request
                       .Url
                       .Authority
                       .Contains("foo.mydomain.com"))
        {
            string URL = 
                (System.Web.HttpContext.Current.Request.Url.Scheme + "://" +
                "foo.mydomain.com" + 
                HttpContext.Current.Request.Url.AbsolutePath + 
                HttpContext.Current.Request.Url.Query);
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.StatusCode = 301;
            System.Web.HttpContext.Current.Response.Status 
                                                    = "301 Moved Permanently";
            System.Web.HttpContext.Current.Response.RedirectLocation = URL;
            System.Web.HttpContext.Current.Response.End();
        }
    }
}

然后我为我的HttpModule添加了一些代码n Web.config

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <modules>
    <add name="MyModule1" type="rewrite.MyModule1" />
  </modules>
  <handlers>
    <remove name="UrlRoutingHandler" />
  </handlers>
</system.webServer>


<system.web>
    <httpModules>
      <add name="MyModule1" type="rewrite.MyModule1" />
    </httpModules>
</system.web>

然后在主机文件中添加以下代码。

127.0.0.1  foo.mydomain.com

然后我为我的网站添加了绑定

在此处输入图片说明

您可以使用IIS7 +的URL重写模块来实现您所描述的内容。 文档在这里: URL重写模块配置参考

简化示例为:

  1. 重写URL,以便foo.mydomain.com访问/ foo / hello(因此没有重定向)
  2. 将网址www.mydomain.com/foo重定向到foo.mydomain.com/foo

在您的web.config, system.webServer节点rewrite部分中添加:

...
    <rewrite>
      <rules>
        <rule name="Redirect mydomain.com/foo to foo.mydomain.com/foo" patternSyntax="Wildcard" stopProcessing="true">
          <match url="foo" negate="false" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="mydomain.com" />
          </conditions>
          <action type="Redirect" url="http://foo.mydomain.com/{R:0}" redirectType="Temporary" />
        </rule>
        <rule name="Rewrite foo.mydomain.com to access /foo/hello" patternSyntax="Wildcard" stopProcessing="true">
          <match url="" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="foo.mydomain.com" />
          </conditions>
          <action type="Rewrite" url="/foo/hello" />
        </rule>
      </rules>
  </rewrite>
</system.webServer>

您也可以使用GUI在IIS管理器中编辑/查看这些规则。 单击您的网站,然后转到“ IIS”部分/“ URL重写”模块。 在下面您可以在GUI中看到第一条规则。

IIS管理器中的URL重写规则

暂无
暂无

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

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