简体   繁体   中英

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

I have an ASP.NET MVC 4 web app. It has a default home page that handles all requests to http://www.mydomain.com as well as http://mydomain.com . I also have an MVC Area set up that can be accessed via www.mydomain.com/Foo/Hello/ (where "Foo" is the name of the area and "Hello" is a controller in that area).

If someone makes a request to foo.mydomain.com right now, it will route them to the default home page controller. I would like all requests to the foo subdomain root (eg. without a specific area/controller specified) to automatically be rerouted to the /foo/hello/ controller.

Additionally, I want any requests to the "Foo" Area via the www subdomain to be redirected to the "foo" subdomain. ie. I want all requests to www.mydomain.com/Foo/Goodbye to be automatically redirected to foo.mydomain.com/Foo/Goodbye

I have, of course, looked at lots and lots of other samples / questions , but none of them seem to address my exact issue.

I'm not sure if I should solve this issue with routes, route constraints, route handlers, etc.

Additionally: this application is hosted on Windows Azure Cloud Services , so I don't have full control over IIS settings.

In your Web Server, the Application Pool of your site must have Integrated PipeLine Mode as highlighted below..

在此处输入图片说明

or you can find it through the Basic settings of your Application Pool like below..

在此处输入图片说明

Then I added the HttpModule in my sample MVC application

HttpModule

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();
        }
    }
}

Then i added some code n Web.config for my HttpModule

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

Then in the host file I added the following code.

127.0.0.1  foo.mydomain.com

Then I added the Bindings for my website

在此处输入图片说明

You can use URL Rewrite module for IIS7+ to achieve what you described. Documentation is here: URL Rewrite Module Configuration Reference .

Simplified example would be:

  1. Rewrite URL so that foo.mydomain.com access /foo/hello (so no redirect)
  2. Redirect URL www.mydomain.com/foo to foo.mydomain.com/foo

Add in your web.config, system.webServer node rewrite section:

...
    <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>

You can also edit/view those rules in IIS Manager, using GUI. Click your web site and go to IIS section / URL Rewrite module. Bellow you can see 1st rule in GUI.

IIS管理器中的URL重写规则

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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