简体   繁体   English

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

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

I have read a nice article about how I can use feature folder structure in my ASP.NET Core MVC application. 我读了一篇不错的文章,关于如何在ASP.NET Core MVC应用程序中使用功能文件夹结构。 My plan is to use then a feature folder structure to organize my web application in a better way. 我的计划是使用功能文件夹结构来更好地组织我的Web应用程序。 First of all lets see my folder structure: 首先让我们看看我的文件夹结构:

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

I have followed the article and I implemented the IViewLocationExpander like the following: 我已经阅读了这篇文章,并实现了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;
    }
}

I placed my main Claims.cshtml view in the "/Claims/Views/" folder. 我将我的Claims.cshtml主视图放置在“ / Claims / Views /”文件夹中。 At the beginning of my Claims.cshtml I have to following line to render my partial view: 在Claims.cshtml的开头,我必须执行以下行来呈现我的局部视图:

@Html.Partial("_NewClaimPopup");

As for the _NewClaimPopup.cshtml, it placed it into the path "/Claims/Views/Shared". 至于_NewClaimPopup.cshtml,它将其放置在路径“ / Claims / Views / Shared”中。 But unfortunately I got the following exception when trying to GET the following url: http://localhost:13078/Claims/Claims 但是不幸的是,尝试获取以下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

It seems that the custom paths are successfully added by the implementation of IViewLocationExpander. 似乎自定义路径已通过IViewLocationExpander的实现成功添加。

Additional infos: 其他信息:

  1. What I also tried is to use "~" sign in paths of the implementation of IViewLocationExpander, so: "~/Claims/Views/{0}.cshtml" and "~/Claims/Views/Shared/{0}.cshtml" but it does not help. 我还尝试了在IViewLocationExpander的实现路径中使用“〜”符号,因此:“〜/ Claims / Views / {0} .cshtml”和“〜/ Claims / Views / Shared / {0} .cshtml”但这无济于事。
  2. I tried to use absolute path for rendering my partial view, but still nothing 我试图使用绝对路径来渲染我的局部视图,但还是没有

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

And of course, I registered my expander in the Startup.cs: 当然,我在Startup.cs中注册了我的扩展器:

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

Last but not least I attach a picture about my project structure: 最后但并非最不重要的一点是,我附上有关我的项目结构的图片: 在此处输入图片说明

Any other idea? 还有其他想法吗? Thanks in advance for any help! 在此先感谢您的帮助!

I have made sample application. 我已经申请了样品。 You can download from below link Custom View Location 您可以从以下链接下载“自定义视图位置”

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

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