分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

MVC自定义视图引擎地址

发布时间:2023-09-06 01:34责任编辑:彭小芳关键词: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()); ???????}  

效果图

MVC自定义视图引擎地址

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved