• asp.net mvc项目自定义区域


    前言

    直接上干货就是,就不废话了。
    使用场景:分离模块,多站点等~~

    一、分离模块

    自定义视图引擎,设置视图路径格式
    项目结构图

    1.Code: 在Global.asax Application_Start方法中添加自定义的视图引擎

            using System.Collections.Generic;
            using System.Web.Mvc;
            namespace MvcProjectMain.AreasViewEngine
            {
    
                /// <summary>       
                /// 自定义视图引擎
                /// </summary>
                /// <remarks>
                ///  ViewEngines.Engines.Add(new MvcProjectMain.AreasViewEngine.ThemableRazorViewEngine());
                /// </remarks>
                public class ThemableRazorViewEngine : VirtualPathProviderViewEngine
                {
                    //所有区域分离到Modules文件夹,{2}为区域名
                    public ThemableRazorViewEngine()
                    {
                        ViewEngines.Engines.Clear();
                        AreaViewLocationFormats = new[]
                        {
                        "~/Modules/{2}/Views/{1}/{0}.cshtml", 
                        "~/Modules/{2}/Views/Shared/{0}.cshtml",
                        };
    
                        AreaMasterLocationFormats = new[]
                        {
                        "~/Modules/{2}/Views/{1}/{0}.cshtml", 
                        "~/Modules/{2}/Views/Shared/{0}.cshtml",
                        };
    
                        AreaPartialViewLocationFormats = new[]
                        {
                        "~/Modules/{2}/Views/{1}/{0}.cshtml", 
                        "~/Modules/{2}/Views/Shared/{0}.cshtml", 
                        };
    
                        ViewLocationFormats = new[]
                        {
                        "~/Views/{1}/{0}.cshtml", 
                        "~/Views/Shared/{0}.cshtml",
                        };
    
                        MasterLocationFormats = new[]
                        {
                        "~/Views/{1}/{0}.cshtml", 
                        "~/Views/Shared/{0}.cshtml", 
                        };
                        PartialViewLocationFormats = new[]
                        {
                        "~/Views/{1}/{0}.cshtml", 
                        "~/Views/Shared/{0}.cshtml", 
                        };
    
                        FileExtensions = new[] { "cshtml" };
                    }
    
                    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
                    {
                        string layoutPath = null;
                        var runViewStartPages = false;
                        IEnumerable<string> fileExtensions = base.FileExtensions;
                        return new RazorView(controllerContext, partialPath, layoutPath, runViewStartPages, fileExtensions);
                    }
    
                    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
                    {
                        string layoutPath = masterPath;
                        var runViewStartPages = true;
                        IEnumerable<string> fileExtensions = base.FileExtensions;
                        return new RazorView(controllerContext, viewPath, layoutPath, runViewStartPages, fileExtensions);
                    }
                }
            }
    

    2.Code:在Global.asax中添加注册区域-->AreaRegistration.RegisterAllAreas();

            public class ThemesAreaRegistration : AreaRegistration
                {
                    public override string AreaName
                    {
                        get
                        {
                            return "MvcProjectThemes";
                        }
                    }
    
                    public override void RegisterArea(AreaRegistrationContext context)
                    {
                        context.MapRoute(
                            "MvcProjectThemes",
                            "MvcProjectThemes/{controller}/{action}/{id}",
                            new { controller = "Home", action = "Index", id = UrlParameter.Optional, },
                            namespaces: new string[] { "MvcProjectThemes.Controllers" }
                        );
                    }
                }
    

    3.Code:注册主项目MvcProjectMain的路由 RouteConfig.RegisterRoutes(RouteTable.Routes);

    namespace MvcProjectMain
    {
        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                    namespaces: new string[] { "MvcProjectMain.Controllers" }
                );
            }
        }
    }
    

    最后

    主要代码就是步骤1中的ThemableRazorViewEngine.cs类。自定义查找路径,其他的都是MVC的基础知识了,不懂自行查阅资料

  • 相关阅读:
    单源最短路——SPFA算法(Bellman-Ford算法队列优化)
    A Compatible Pair
    欧拉函数知识点总结及欧拉函数打表代码(数论)
    Fafa and the Gates(模拟)
    C语言--链表基础模板
    路由器如何设置上网(TP-LINK)
    学员成绩管理系统
    Bad Cowtractors(最大生成树)
    Agri-Net(最小生成树)
    Median Weight Bead(最短路—floyed传递闭包)
  • 原文地址:https://www.cnblogs.com/morang/p/mvcmodules.html
Copyright © 2020-2023  润新知