簡體   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