asp.net 实现 动态二级域名绑定 mvc方式实现

发布时间2019/7/4 9:05:02

下载次数 0 次 访问量91 次

免开发-应用中心上线啦,专注提供优质插件源码和产品应用!

利用 mvc中area实现 动态二级域名 

实现效果:   http://{area}.example.com  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using HB.Controllers;
 
namespace HB.MultiSite
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
    // 请访问 http://go.microsoft.com/?LinkId=9394801
 
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
 
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            routes.Add("DomainRouteForManage", new DomainRoute(
                "manage.8fdc.com",                             // 固定的二级域名
                "{controller}/{action}/{id}",                  // URL with parameters
                new
                {
                    area = "Manage",
                    controller = "Home",
                    action = "Index",
                    id = "",
                    Namespaces = new string[] { "HB.Controllers.Manage" }
                }  // Parameter defaults
            ));
 
            routes.Add("DomainRouteForMutiWebSite", new DomainRoute(
                "{area}.8fdc.com",                             // {area}作为二级域名
                "{controller}/{action}/{id}",                  // URL with parameters
                new
                {
                    area = "cs",
                    controller = "Home",
                    action = "Index",
                    id = "",
                    Namespaces = new string[] { "HB.Controllers.WebSite" }
                }  // Parameter defaults
            ));
 
            routes.MapRoute(
                "Default", // 路由名称
                "{area}/{controller}/{action}/{id}", // 带有参数的 URL
                new
                {
                    area = "cs",
                    //controller = @"[a-z]{4,}", //controller必须为英文字符,最少长度为4 
                    //action = @"[a-z]{4,}", //action必须为英文字符,最少长度为4
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                },
                null,
                new string[] { "HB.Controllers.WebSite" } // 参数默认值
              );
 
            //routes.MapRoute(
            //    "Default", // 路由名称
            //    "{controller}/{action}/{id}", // 带有参数的 URL
            //    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
            //);
 
        }
 
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
 
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
 
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);  //查看路由信息
        }
    }
}

网友评论

发表评论

欢迎评论哦!