在 Blazor 服务器端应用程序中查找 cshtml 文件时,使用的是视图引擎,而不是 Razor。因此,需要使用视图引擎来查找 cshtml 文件。以下是一个使用视图引擎来查找 cshtml 文件的示例。
首先,需要创建一个自定义的视图引擎,并将其注册到服务中。
using Microsoft.AspNetCore.Mvc.ViewEngines;
public class MyViewEngine : IViewEngine
{
public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
{
// add your own logic to find the view file.
}
public ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage)
{
// add your own logic to get the view file.
}
}
接下来,将此视图引擎注册到 Blazor 服务器端应用程序中的服务中。
builder.Services.AddSingleton();
最后,在需要查找 cshtml 文件的地方调用 FindView 方法,并指定正确的视图名称。
var viewEngine = serviceProvider.GetService();
var viewResult = viewEngine.FindView(httpContext, "MyView.cshtml", false);