ASP.NET Core具有用於重用視圖部分的View Components機制。您可以在使用Component.InvokeAsync
通話剃刀模板文件中的視圖組件:如何在ASP.NET Core中的剃鬚刀視圖中呈現之前檢查視圖組件是否存在
@await Component.InvokeAsync("MyComponent", new { data = 1 })
如果考慮到與給定的名稱組件不存在的InvalidOperationException
拋出異常。
InvalidOperationException: A view component named 'MyComponent' could not be found.
我想知道如何查看組件是否存在之前呈現在剃刀視圖。理想的情況是這樣的:
@if (Component.Exists("MyComponent")
{
@await Component.InvokeAsync("MyComponent", new { data = 1 })
}
else
{
<p>Component not found</p>
}
這證實了我得到的它找不到的錯誤,但我不知道爲什麼,因爲它沒有給出任何其他線索,爲什麼它沒有找到組件。見http://stackoverflow.com/questions/43902264/viewcomponent-in-external-assembly-cannot-be-found –