【问题标题】:asp.net core mvc displays empty pageasp.net core mvc 显示空白页面
【发布时间】:2020-02-17 04:39:31
【问题描述】:

拜托,我需要帮助解决一个与 asp.net 核心 mvc 应用程序有关的烦人问题。

该应用程序仅显示空白主页 - 没有任何内容,包括 html 标记。即使直接在浏览器上输入 url 也不会调用控制器并且不会显示错误。

我已经重新创建了几次新应用程序,结果相同。另外,我在 Startup 类的 Configure 方法中添加了以下语句,但无济于事。

app.UseDefaultFiles();
app.UseStaticFiles();
app.UseIdentity();

任何解开这个谜团的指南都将受到高度赞赏。

谢谢。

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        //add NLog to ASP.NET Core
        //loggerFactory.AddNLog();

        ////add NLog.Web
        //app.AddNLogWeb();

        //needed for non-NETSTANDARD platforms: configure nlog.config in your project root
        //env.ConfigureNLog("nlog.config");

        if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseBrowserLink();
        } else {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseDefaultFiles();
        app.UseStaticFiles();
        app.UseIdentity();

        // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
        app.UseMvcWithDefaultRoute();
        //app.UseMvc(routes => {
        //    routes.MapRoute(
        //        name: "default",
        //        template: "{controller=Home}/{action=Index}/{id?}");
        //});

        // For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
        try {
            using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
                .CreateScope()) {
                serviceScope.ServiceProvider.GetService<ChurBaseContext>()
                     .Database.Migrate();

                var userManager = serviceScope.ServiceProvider.GetService<UserManager<ChurchMember>>();
                var roleManager = serviceScope.ServiceProvider.GetService<RoleManager<IdentityRole>>();

                serviceScope.ServiceProvider.GetService<ChurBaseContext>().EnsureSeedData(userManager, roleManager);
            }
        } catch { }
    }

【问题讨论】:

    标签: asp.net-core-mvc


    【解决方案1】:

    当我在 Startup.cs 中的 UseMvc() 上方设置 Use() 扩展时,我遇到了这种行为,它没有调用“await next();”在函数的末尾。

    app.Use(async (context, next) =>
    {
        // some custom magic
    
        await next();// don't forget this
    });
    
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
    

    【讨论】:

      【解决方案2】:

      您可以尝试添加:

      app.UseStatusCodePages();
      

      app.UseStatusCodePagesWithReExecute("/error/{0}");
      

      有关后者的信息,您可以查看: [When using UseStatusCodePagesWithReExecute, statuscode is not sent to browser

      【讨论】:

        【解决方案3】:
        app.UseMvcWithDefaultRoute(); //in startup.cs
        

        app.UseMvc(routes =>
        {
          routes.MapRoute(
            name: "default",
            template: "{controller}/{action}/{id?}",
            defaults: new { controller = "Home", action = "Index" });
        });
        

        【讨论】:

        • 如前所述,我已准备好一切,但仍然遇到同样的问题。仍然不确定我做错了什么。谢谢。
        【解决方案4】:

        这可能是因为您没有将 MVC 用于执行管道。

        在启动类的Configure方法中,需要这样添加-

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
                {
                    ......
        
                    app.UseStaticFiles();
        
                    app.UseMvc(routes =>
                    {
                        routes.MapRoute(
                            name: "default",
                            template: "{controller=Home}/{action=Index}/{id?}");
                    });
                }
        

        或者像这样添加 UseMvcWithDefaultRoute-

        app.UseMvcWithDefaultRoute();
        

        这基本上将 MVC 添加到 IApplicationBuilder 请求执行管道中。

        看看这是否有帮助。

        【讨论】:

        • 另外,检查你的浏览器控制台,你会看到这样的错误-Failed to load resource: the server responded with a status of 404 (Not Found)
        • 感谢您的快速回复。我已经按照说明设置了所有内容,但没有区别。浏览器控制台也没有错误。
        • 能分享完整的启动配置方法吗?
        • 我刚做了。谢谢。
        • 我有同样的问题。使用 IISExpress 在 Windows 10 下一切正常,但不适用于 apache/centos。
        【解决方案5】:

        当您只看到一个空白页面而除了空白页面之外没有任何反应时,可能是由多种原因引起的。这是我见过的两个:

        1. 损坏的 web.config
        2. 与 IIS 冲突的 web.config。当您的 IIS 配置中锁定特定部分时,可能会发生这种情况。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-26
          • 2013-03-03
          • 2011-12-27
          相关资源
          最近更新 更多