呈现服务器端命令时的Razor白色页面(Asp.net 5)

本文关键字:Asp net 白色 服务器端 命令 Razor | 更新日期: 2023-09-27 18:26:27

首先抱歉我的英语不好。

当我尝试查看HomeControllerIndex操作的url /时,我得到的只是一个白色页面。

我从空的ASP.NET 5 Web应用程序模板开始,然后添加Controllers'HomeController.cs。接下来,我将在Views and finally adding Index.chtml under Views''Home`下添加Home文件夹。

当我使用TempData["isim"]时,它会显示一个空白页面。

为什么什么都没出现?

启动.cs:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace WebApplication17
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc(routes => routes.MapRoute("default", "{controller=Home}/{ation=Index}"));
        }
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

Index.chtml(工作):

<div>
    my welcome page
</div>

Index.chtml(不工作):

<div>
    my welcome page
    @{
        <input type="text" id="isim" />
        string ad = TempData["isim"] as string;
        <input type="submit" />
    }
</div>

屏幕捕获

呈现服务器端命令时的Razor白色页面(Asp.net 5)

可能正在发生异常。

请在Startup.csConfigure()方法中添加app.UseDeveloperExceptionPage();

样本:

using Microsoft.AspNet.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace HelloMvc
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {                
            app.UseDeveloperExceptionPage(); // <=== that is missing
            app.UseMvcWithDefaultRoute();
        }
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}

非常感谢@Maxime Rouiller。我在你的帮助下解决了这个问题。如果我不能添加开发人员异常捕获错误,我就看不到问题,也找不到解决方案。

解决方案如下代码所示:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace WebApplication17
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(); 
            services.AddSession(); // problem and solution is here
            services.AddCaching(); // problem and solution is here
        }
        public void Configure(IApplicationBuilder app)
        {
            app.UseDeveloperExceptionPage(); //thanks Maxime Rouiller.
            app.UseSession(); // problem and solution is here
            app.UseMvc(routes => routes.MapRoute("default", "{controller=Home}/{action=Index}"));
        }
        public static void Main(string[] args) => WebApplication.Run<Startup>(args);
    }
}