简体   繁体   English

ASP.NET Core 6 中的功能文件夹结构

[英]Feature Folder Structure in ASP.NET Core 6

How to create feature folder structure in ASP.NET Core 6 web project similar to one described in following article: https://scottsauber.com/2016/04/25/feature-folder-structure-in-asp-net-core/ ?如何在 ASP.NET Core 6 Web 项目中创建类似于以下文章中描述的功能文件夹结构: https ://scottsauber.com/2016/04/25/feature-folder-structure-in-asp-net-core/ ?

在此处输入图像描述

Project structure:项目结构:

  • WebPublic网络公共
  • WebCommon网络通用

following code was working in Asp.net Core 2.1 in "WebCommon" project which is added as reference in "WebPublic" project.以下代码在“WebCommon”项目中的 Asp.net Core 2.1 中工作,该项目作为参考添加到“WebPublic”项目中。

在此处输入图像描述

IViewLocationExpander Interface can be used if Nuget package "Microsoft.AspNetCore.Mvc.Razor" is referenced in project, but latest version (updated 12/4/2018) is only 2.2.0.如果项目中引用了 Nuget 包“Microsoft.AspNetCore.Mvc.Razor”,则可以使用 IViewLocationExpander 接口,但最新版本(2018 年 12 月 4 日更新)仅为 2.2.0。

Following error occurs even if in "WebCommon" project "Microsoft.AspNetCore.Mvc.Razor" is referenced:即使在“WebCommon”项目中引用了“Microsoft.AspNetCore.Mvc.Razor”,也会出现以下错误:

Could not load type 'Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler' Version=6.0.0.0无法加载类型“Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler”版本=6.0.0.0

Here is a solution for .NET6.这是 .NET6 的解决方案。 You can skip the approach with ViewLocationExapnder and just exapnd on how the views are located by configuring the RazorViewEngineOptions as (for example, in your Program.cs where services is WebApplicationBuilder builder.Services):您可以跳过使用 ViewLocationExapnder 的方法,并通过将RazorViewEngineOptions配置为(例如,在您的 Program.cs 中,其中服务是WebApplicationBuilder builder.Services)来解释视图的位置:


// using Microsoft.AspNetCore.Mvc.Razor;

services.Configure<RazorViewEngineOptions>(options =>
{
    options.ViewLocationFormats.Add($"/Features/Codebooks/{{1}}/{{0}}{RazorViewEngine.ViewExtension}");
    options.ViewLocationFormats.Add($"/Views/Shared/{{0}}{RazorViewEngine.ViewExtension}");
});

Here I configured it to look into Features/Codebooks/{controllerName}/{actionName}.cshtml, and that works very well.在这里,我将其配置为查看 Features/Codebooks/{controllerName}/{actionName}.cshtml,效果非常好。

显示功能文件夹的 C# 代码和解决方案资源管理器的 Visual Studio 屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM