码迷,mamicode.com
首页 > Web开发 > 详细

MVC自定义视图引擎地址

时间:2018-01-05 01:19:14      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:protected   alt   mapr   顺序   dex   manage   optional   wpa   image   

先看结构

技术分享图片

 

1、RouteConfig 文件(注意顺序)

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            
            routes.MapRoute(
               name: "Manage_Default",
               url: "Manage/{controller}/{action}/{id}",
               defaults: new { controller = "Demo", action = "Index", id = UrlParameter.Optional },
               namespaces: new string[] { "Ku_MVC.Controllers.Manage" }
           );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

  2、新增文件 MyRazorViewEngine

public class MyRazorViewEngine : RazorViewEngine
    {
        public MyRazorViewEngine()
            : base()
        {
            ViewLocationFormats = new[] {  
                 "~/Views/{1}/{0}.cshtml",
                 "~/Views/Manage/{1}/{0}.cshtml",
            };

        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            return base.CreatePartialView(controllerContext, partialPath);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            return base.CreateView(controllerContext, viewPath, masterPath);
        }

        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            return base.FileExists(controllerContext, virtualPath);
        }
    }

  3、Global.asax 

 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
             
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            RegisterView();
        }
        protected void RegisterView()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new Controllers.MyRazorViewEngine());
        }  

 

效果图

技术分享图片

 

MVC自定义视图引擎地址

标签:protected   alt   mapr   顺序   dex   manage   optional   wpa   image   

原文地址:https://www.cnblogs.com/LoveTX/p/8196540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!