MVC 3子域路由 [英] MVC 3 Subdomain Routing

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

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/278668/is-it-possible-to-make-an-asp-net-mvc-route-based-on-a-subdomain\">Is有可能使基于子域的ASP.NET MVC路线?

在asp.net MVC 3网站,我想为用户创建在线商店。
由用户创建的任何一家商店应该有类似URL的 SHOPNAME .mydomain.com来

In asp.net MVC 3 site i'd like to create online stores for users. Any store that is created by user should have a URL like "shopname.mydomain.com".

我尝试了一些路由工作,但都失败了。我研究了一个解决方案,但找不到任何妥善的解决办法。

I tried some routing work but failed at all. I am researching for a solution but cannot find any proper solution.

我的目的就是;如果我可以添加一个途径来管理试图找到一个子我会检查任何请求,如果它是一个用户的网上商店名,并获得比赛的动态数据。

My purpose is that; if I can add a route to manage any request that tries to find a subdomain I will check if it is a user online shop name and get the dynamic data on play.

需要路由的帮助:)谢谢。

Need routing help :) Thanks.

推荐答案

我已经找到了一个非常强大的方式。所以检查:)

I have found a very powerful way. So check this :)

所有的Visual Studio应用程序开发服务器首先你必须编辑'主人'的文件。

First of all for application development server of visual studio you have to edit the 'hosts' file.

打开记事本作为管理员。添加任何名称为您的域名类似

Open notepad as administrator. Add any name for your domain something like

127.0.0.1 mydomain.com
127.0.0.1 sub1.mydomain.com

127.0.0.1 mydomain.com 127.0.0.1 sub1.mydomain.com

和需要在开发使用什么。

and what you need to use on development.

后给予特定端口号到Web项目。例如,45499。通过这种方式,您将能够仙请求您的项目在浏览器中写:

After give a specific port number to your web project. For example "45499". By this way you will be able to sen request to your project by writing in browser :

mydomain.com:45499
要么
sub1.mydomain.com:45499

mydomain.com:45499 or sub1.mydomain.com:45499

这是preparing一步。让我们得到了答案。

That was the preparing step. Lets get on the answer.

通过使用您可以创建您的路线限制了 IRouteConstraint 类。

By using the IRouteConstraint class you can create your route constrains.

public class SubdomainRouteConstraint : IRouteConstraint
{
    private readonly string SubdomainWithDot;

    public SubdomainRouteConstraint(string subdomainWithDot)
    {
        SubdomainWithDot = subdomainWithDot;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var url = httpContext.Request.Headers["HOST"];
        var index = url.IndexOf(".");

        if (index < 0)
        {
            return false;
        }
        //This will bi not enough in real web. Because the domain names will end with ".com",".net"
        //so probably there will be a "." in url.So check if the sub is not "yourdomainname" or "www" at runtime.
        var sub = url.Split('.')[0];
        if(sub == "www" || sub == "yourdomainname" || sub == "mail")
        {
            return false;
        }

        //Add a custom parameter named "user". Anything you like :)
        values.Add("user", );
        return true;
    }
}

和添加约束你想使用的任何途径。

And add your constrain in any route you would like to use.

routes.MapRoute(
                    "Sub", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "SubdomainController", action = "AnyActionYouLike", id = UrlParameter.Optional },
                    new { controller = new SubdomainRouteConstraint("abc.") },
                    new[] { "MyProjectNameSpace.Controllers" }
                    ); 

您的默认路由之前将这个路线。这就是全部。

Put this routes before your default route. That's all.

在你可能不喜欢支票子域名的任何约束为客户店铺名称或什么的。

In the constraint you may do anything like check for subdomain name is a client shop name or whatever.

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

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