简体   繁体   中英

URL Routing across multiple subdomains

I find myself in a difficult situation. We're working on an ASP.NET MVC 2 application which is comprised of multiple sections. It is a design goal to have these sections span across several subdomains. Each subdomain will have its own controller.

The challenge is that our hosting provider's control panel allows two forms of redirection for subdomains, and neither of them seem to fit the bill. The choices are:

  • Redirecting to URL. Choice is given whether to redirect to an exact destination or a destination relative to the request URL.

  • Redirecting to a specific folder in my hosting space.

I'll try to illustrate the intended behaviour. Assuming the default route is {controller}/{action}/{id} , I'd like the URL http://subdomain.welcome.com/a/b be handled by the MVC Application like http://welcome.com/subdomain/a/b .

The URL redirection could solve this problem, except for the fact that the user sees a URL change occur in the browser. We don't want the client to see the redirection occur.

Redirecting to our MVC apps root folder doesn't work at all. The app doesn't pick up the request and a 4xx error gets passed back by IIS.

edit:

In the interest of finding an answer, I'll simplify this a bit. The "redirect to URL" doesn't do what I want so that leaves redirecting to a folder.

If I'm redirecting a subdomain to the root folder of my MVC App and IIS wont pick up the requests, is this a limitation of IIS or my provider?

Can you make your hosting website host headers respond to *.mydomain.com ? Meaning, can your website take request for any sub domain of your primary domain? If so, then reference this post on how to handle subdomain routing in MVC apps and you should be good to go.

I would update the code in the post to this however, to make the code more succinct. In any case, make sure you have your 404 errors in place for people attempting to go to subdomains that don't exist.

public class ExampleRoute : RouteBase
{

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var url = httpContext.Request.Headers["HOST"];
        var index = url.IndexOf(".");

        if (index < 0)
            return null;

        var subDomain = url.Substring(0, index);

            var routeData = new RouteData(this, new MvcRouteHandler());
            routeData.Values.Add("controller", subdomain); //attempts to go to controller action of the subdomain
            routeData.Values.Add("action", "Index"); //Goes to the Index action on the User2Controller

            return routeData;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        //Implement your formating Url formating here
        return null;
    }
}

Not sure if this is overkill (this is actually used to serve pages from a zip file or resource file, etc), BUT... perhaps you could use a Virtual Path Provider?..

Implement a class that inherits from VirtualPathProvider, and register it in global startup like so:

HostingEnvironment.RegisterVirtualPathProvider(new MyVirtualPathProvider());

Then implement a class that inherits from VirtualFile and serve it from the GetFile() override in your virtual path provider implementation:

public override VirtualFile GetFile( string virtualPath )
{
 if( IsVirtualFile(virtualPath) )
    return new MyVirtualFile(virtualPath);
 return base.GetFile(virtualPath);
}

Note: IsVirtualFile is a function you would have to implement, based on the rules you have regarding the URL format, etc.

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