如果ASP.NET Core无法通过路径找到cshtml文件,可以尝试以下解决方法:
确保cshtml文件位于正确的位置:确保cshtml文件存在于正确的文件夹中,通常是在项目的"Views"文件夹中。
更新Razor视图引擎的搜索路径:可以通过在Startup.cs文件的ConfigureServices方法中调用AddRazorRuntimeCompilation方法来更新Razor视图引擎的搜索路径。
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().AddRazorRuntimeCompilation();
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
}).UseViews(options =>
{
options.ViewLocationFormats.Clear();
options.ViewLocationFormats.Add("/Views/{1}/{0}.cshtml");
options.ViewLocationFormats.Add("/Views/Shared/{0}.cshtml");
});
检查文件权限:确保cshtml文件所在的文件夹具有读取权限,以及应用程序对该文件夹具有访问权限。
检查文件扩展名:确保cshtml文件的扩展名是.cshtml而不是其他格式。
通过上述方法,你应该能够解决ASP.NET Core无法通过路径找到cshtml文件的问题。