繁体   English   中英

ASP.NET Core MVC:在自定义功能文件夹结构中找不到部分视图

[英]ASP.NET Core MVC: The partial view was not found in a custom feature folder structure

我读了一篇不错的文章,关于如何在ASP.NET Core MVC应用程序中使用功能文件夹结构。 我的计划是使用功能文件夹结构来更好地组织我的Web应用程序。 首先让我们看看我的文件夹结构:

...
wwwroot
Claims
   Controllers
   Services
   Views
      Shared
Map
...

我已经阅读了这篇文章,并实现了IViewLocationExpander,如下所示:

public class MyViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context)
    {
        context.Values["customviewlocation"] = nameof(MyViewLocationExpander);
    }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        var viewLocationFormats = new[]
        {
            "/Claims/Views/{0}.cshtml",
            "/Claims/Views/Shared/{0}.cshtml"
        };
        return viewLocationFormats;
    }
}

我将我的Claims.cshtml主视图放置在“ / Claims / Views /”文件夹中。 在Claims.cshtml的开头,我必须执行以下行来呈现我的局部视图:

@Html.Partial("_NewClaimPopup");

至于_NewClaimPopup.cshtml,它将其放置在路径“ / Claims / Views / Shared”中。 但是不幸的是,尝试获取以下URL时出现以下异常: http:// localhost:13078 / Claims / Claims

InvalidOperationException: The partial view '_NewClaimPopup' was not found. The following locations were searched:
/Views/Claims/_NewClaimPopup.cshtml
/Views/Shared/_NewClaimPopup.cshtml

似乎自定义路径已通过IViewLocationExpander的实现成功添加。

其他信息:

  1. 我还尝试了在IViewLocationExpander的实现路径中使用“〜”符号,因此:“〜/ Claims / Views / {0} .cshtml”和“〜/ Claims / Views / Shared / {0} .cshtml”但这无济于事。
  2. 我试图使用绝对路径来渲染我的局部视图,但还是没有

    @ Html.Partial( “” 〜/宣称/查看/ _NewClaimPopup.cshtml“);

当然,我在Startup.cs中注册了我的扩展器:

    services.Configure<RazorViewEngineOptions>(options => options.ViewLocationExpanders.Add(new MyViewLocationExpander()));

最后但并非最不重要的一点是,我附上有关我的项目结构的图片: 在此处输入图片说明

还有其他想法吗? 在此先感谢您的帮助!

我已经申请了样品。 您可以从以下链接下载“自定义视图位置”

暂无
暂无

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

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