Views Mvc multi-level directory asp.net mvc4 routing rewrite and modify the view looking view of the rules

In general we mvc development process, will encounter this problem. Pages are always written in the Views folder, but also only a Controller page can only be written under the name corresponding to the Controller folder named. If we wrote it somewhere else? It will certainly be an error. This is a provision in a convention mvc and must write.

1. Normal project directory, as follows:

      

      We want to access the Index page, just enter the Home / Index can be visited. We were able to access this, because we are at the beginning of the project to create a system to the default configuration of a default route. We can be accessed in accordance with this default routing rules.

2. Then visit the way we look at what we need, as shown below

      TestController inside Index page if we want to access the Admin under, then we enter the Test / Index, certainly not this. Because TestController simply not in the root directory Controllers, but under Controllers / Admin, so we simply can not find this Test Controller. Then we enter the Admin / Test / Index, then we need to add a routing configuration, because the default route previously only through {Controller} / {Action} / {Id} access in this way, is the need to start with a Controller. We reconfigured the routing as follows:

复制代码
public static void RegisterRoutes(RouteCollection routes)
         {
             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
             
             //路由规则匹配是从上到下的,优先匹配的路由一定要写在最上面。因为路由匹配成功以后,他不会继续匹配下去。
             routes.MapRoute(
                "Admin", // 路由名称,这个只要保证在路由集合中唯一即可
                "Admin/{controller}/{action}/{id}", //路由规则,匹配以Admin开头的url
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 
            );
 
             routes.MapRoute(
                 "Default", // Route name
                 "{controller}/{action}/{id}", // URL with parameters
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
             );
 
     }
复制代码

 那么我们这个时候再次输入Admin/Test/Index,能找到Views/Admin/Test/Index.cshtml这个页面吗?显然是不能的,因为除了路由配置怎么访问Controller外,寻找Views里面的页面也有自己的规则。测试结果肯定是找不到页面,我们看看错误信息就知道他是怎么寻找cshtml页面了。

    

    

The following locations were searched” 翻译过来就是"以下地址被搜索过"。那么他只搜索这些地址,这里我只写razor视图的地址,写成通配符就是:

  • Views/{1}/{0}.cshtml
  • Views/Shared/{0}.cshtml

{1}表示Controller的名称,{0}表示视图名称,Shared是存放模板页的文件夹。一看就很清楚了。这个就是寻找视图的规则,所以我们存放在Admin/Test/Index.cshtml的存放规则就不满足。那么我们修改下,如下图:

  直接将Test文件夹存放在Views下面,那么我们就满足这个寻找视图的规则了,我们输入Admin/Test/Index,也确实访问成功了。

    

但是这个方式的存储肯定不是我们需要的,既然我们Controller区分存放了,我们肯定也希望Views也能够这样存放的。

3.那么我们进入正题,修改他的寻找视图的规则,让他能够按照我们的规则来访问,就像修改路由一样。

在项目中新建立一个cs类MyViewEngine,继承RazorViewEngine。代码如下:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcRoute.MvcEx
{

    public sealed class MyViewEngine : RazorViewEngine
    {

        public MyViewEngine()
        {
            ViewLocationFormats = new[]
            {
                "~/Views/{1}/{0}.cshtml",
                "~/Views/Shared/{0}.cshtml",
                "~/Views/Admin/{1}/{0}.cshtml"//我们的规则
            };
        }
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }

    }
}
复制代码

然后将这个规则注册到系统中,在global中注册一下,这样我们就可以通过自己的方式来访问了。global注册如下:

复制代码
protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            RegisterView();//注册视图访问规则
        }

        protected void RegisterView()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new MyViewEngine());
        }
复制代码

 结果如下图:

    

 

REFERENCE FROM : http://www.cnblogs.com/weixing/p/3326188.html

转载于:https://www.cnblogs.com/zhangchenliang/p/4112355.html

Guess you like

Origin blog.csdn.net/weixin_34148456/article/details/93495468