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

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

问题描述

可能的重复:
是否有可能基于子域制作 ASP.NET MVC 路由?

在asp.net MVC 3 站点中,我想为用户创建在线商店.任何由用户创建的商店都应该有一个类似 "shopname.mydomain.com" 的 URL.

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的应用程序开发服务器,你必须编辑'hosts'文件.

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

这是准备步骤.让我们来看看答案.

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天全站免登陆