zoukankan      html  css  js  c++  java
  • MVC自定义视图引擎地址

    先看结构

    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());
            }  

    效果图

    我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan

  • 相关阅读:
    代码整洁之道-格式
    代码整洁之道-函数
    redis常规命令记录
    周报2019.7.19
    docker mysql安装
    Python requirements.txt
    Javascript-关于null、undefined、空字符串的区分
    Javascript-string-Array
    取出两个二维数组中不重复的数组值方法
    让未知宽高的元素水平垂直居中
  • 原文地址:https://www.cnblogs.com/LoveTX/p/8196540.html
Copyright © 2011-2022 走看看