【问题标题】:ASP.NET Core 5 application cannot find view errorASP.NET Core 5 应用程序找不到视图错误
【发布时间】:2020-12-11 04:36:58
【问题描述】:

我在 Visual Studio 2019 中创建了一个 ASP.NET Core 5 应用程序项目。

要求是视图不应被预编译。

所以我将 RazorCompileOnPublish 设置为 false:

<RazorCompileOnPublish>false</RazorCompileOnPublish>

.csproj 文件中。

发布项目后,将Views文件夹复制到发布目录。

但在运行时,我收到以下错误。

System.InvalidOperationException:未找到视图“索引”。搜索了以下位置:

  /Views/Home/Index.cshtml  
  /Views/Shared/Index.cshtml

在 Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable1 originalLocations) at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result) at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeResultAsync&gt;g__Logged|21_0(ResourceInvoker invoker, IActionResult result) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResultFilterAsync&gt;g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeNextResourceFilter&gt;g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.&lt;InvokeAsync&gt;g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.&lt;Invoke&gt;g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.&lt;Invoke&gt;g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.HandleException(HttpContext context, ExceptionDispatchInfo edi) at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.&lt;Invoke&gt;g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication1 应用程序) 错误:Microsoft.AspNetCore.Server.Kestrel[9] *

program.cs和Startup.cs文件如下:

public class Startup
{        
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration) {
        Configuration = configuration;
    }     

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services) {
        services.AddControllersWithViews();           
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
        if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
        }
        else {
            app.UseExceptionHandler("/Home/Error");
        }          
        app.UseStaticFiles();

        app.UseRouting();
        app.UseAuthorization();

        app.UseEndpoints(endpoints => {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            //endpoints.MapRazorPages(); //Has no effect
        });
    }
}

public class Program {
    public static void Main(string[] args) {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) {
        Console.WriteLine($"ContentRoot set to: {Directory.GetCurrentDirectory()}");
        var hostBuilder = Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder => {
                webBuilder.UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())                    
                .UseStartup<Startup>();
            });
        return hostBuilder;
    }
}

请帮忙。提前致谢

【问题讨论】:

    标签: asp.net-core .net-5


    【解决方案1】:

    此设置可在 .net core 2.x 中使用,但无法在 .net core 5 中启用运行时编译。

    1. 在这种情况下,您可以使用AddRazorRuntimeCompilation 来启用运行时编译。

      public void ConfigureServices(IServiceCollection services)
       {
           services.AddControllersWithViews()
               .AddRazorRuntimeCompilation();
       }
      
    2. 另一种方法是有条件地启用运行时编译。

      public Startup(IConfiguration configuration,IWebHostEnvironment webHostEnvironment)
       {
           Configuration = configuration;
           Env = webHostEnvironment;
       }
       //...
       public IWebHostEnvironment Env { get; set; }
      
       public void ConfigureServices(IServiceCollection services)
       {
           IMvcBuilder builder = services.AddRazorPages();
      
           if (!Env.IsDevelopment())
           {
               builder.AddRazorRuntimeCompilation();
           }
           services.AddControllersWithViews();
       }
      

    你可以参考这个document

    我在编辑器中编辑Index.cshtml

    然后将其保存在发布文件夹中并启动项目。它编译成这样。

    【讨论】:

    • 嗨@Karney,我不希望将视图文件编译为dll。我想编辑发布 Web 应用程序的 .cshtml 帖子。
    • 是的,你可以编辑.cshtml,然后放到view文件夹里,view就会变了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多