跨多个子域网址路由 [英] URL Routing across multiple subdomains

查看:216
本文介绍了跨多个子域网址路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己在一个困难的局面。我们正在从事一个ASP.NET MVC 2应用程序,它由多个部分组成。这是一个设计目标有这些部分在多个子域跨越。每个子都将拥有自己的控制器。

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:


  • 重定向到URL。选择给出是否重定向到一个确切的目的地或相对URL请求的目的地。

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

我会尽力来说明预期的行为。假设默认路由是 {控制器} / {行动} / {ID} ,我想的网址 HTTP://subdomain.welcome。 COM / A / b 由MVC应用程序如 http://welcome.com/subdomain/a/b 处理。

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.

该URL重定向可以解决这个问题,除了用户看到一个URL的变化发生在浏览器中的事实。我们不希望客户看到重定向发生。

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.

重定向到我们的应用程式MVC根文件夹不能在所有工作。该应用程序不拿起请求和4xx错误得到由IIS传递回来。

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.

编辑:

在找到答案的利益,我将简化这一点。 重定向到URL并没有做什么,我想要让叶重定向到一个文件夹中。

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.

如果我重定向子到我的MVC应用程序的根文件夹和IIS不会拿起请求,这是IIS或我的供应商的限制?

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?

推荐答案

您可以让您的托管网站主机头回应 *。mydomain.com ?含义,可以将您的网站采取主域的所有子域名的请求?如果是这样,那么参考<一个href=\"http://stackoverflow.com/questions/278668/is-it-possible-to-make-an-asp-net-mvc-route-based-on-a-subdomain\">this帖子如何处理子路由在MVC应用程序,你应该是好去。

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.

我将更新后的code将该然而,要使$​​ C $ C更加简洁。在任何情况下,确保你有一个地方你的404错误的人试图去不存在的子域。

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

这篇关于跨多个子域网址路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆