目标目录结构如下:
Controllers
stock
StockInController
customer
CustomerInfoController
Views
stock
StockIn
aa.cshtml
customer
CustomerInfo
bb.cshtml
需做以下配置
Startup.cs文件配置如下
1 app.UseEndpoints(endpoints => 2 { 3 endpoints.MapControllerRoute( 4 name: "default", 5 pattern: "{controller=Home}/{action=Index}/{id?}"); //原默认路由配置 6 endpoints.MapControllerRoute( 7 name: "Stock", 8 pattern: "{FolderName=Stock}/{controller=Home}/{action=Index}/{id?}"); //新增的子目录路由配置、其中stock可以换成自己的目录名 9 endpoints.MapControllerRoute( 10 name: "Customer", 11 pattern: "{FolderName=Customer}/{controller=Home}/{action=Index}/{id?}"); //新增的子目录路由配置 12 13 });
控制器中调整如下
1 public ActionResult AddInfo() 2 { 3 var path = "~/Views/" + HttpContext.Request.RouteValues["FolderName"] + "/" + HttpContext.Request.RouteValues["Controller"] + "/" + HttpContext.Request.RouteValues["action"] + ".cshtml"; 4 if (!HttpContext.Request.RouteValues.ContainsKey("FolderName")) //兼容没有新增子目录的,如果全部增加子目录,则此部分可以删掉。 5 { 6 path = "~/Views/" + HttpContext.Request.RouteValues["Controller"] + "/" + HttpContext.Request.RouteValues["action"] + ".cshtml"; 7 } 8 return View(path); 9 }
其中FolderName、Controller、action分别对应前面Startup.cs中配置的参数。以上亲测可用。
标签:core,Views,子目录,MapAreaControllerRoute,Request,action,HttpContext,RouteValues From: https://www.cnblogs.com/acore/p/16926711.html