在ASP.NET 5应用程序上未点击错误页面 [英] Not hitting error page on ASP.NET 5 app

查看:53
本文介绍了在ASP.NET 5应用程序上未点击错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个新的ASP.NET 5 MVC 6应用程序.到目前为止,这是一个空的,即用型的应用程序,它是在选择ASP.NET 5 MVC项目时Visual Studio创建的.

I'm playing with a new ASP.NET 5 MVC 6 app. So far, it's an empty, out-of-the-box application Visual Studio creates when ASP.NET 5 MVC project is selected.

在Home控制器操作中,我添加了一行代码,以查看是否将我重定向到通用错误页面,即〜/Views/Shared/Error.cshtml

In the Home controller action, I added the line to see if I'd get redirected to the generic error page i.e. ~/Views/Shared/Error.cshtml

throw new UnauthorizedAccessException("Test exception!");

运行项目时,我得到的只是代码中的异常,但不会重定向到错误页面.

When I run the project, all I get is an exception in my code but don't get redirected to the error page.

以下是标准的Startup.cs -再次提供了开箱即用的代码.我尚未进行任何更改.

The following is the standard Startup.cs -- again, out-of-the-box code. I didn't make any changes yet.

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

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

    app.UseIISPlatformHandler();

    app.UseStaticFiles();

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

我应该不会重定向到错误页面?

Shouldn't I get redirected to the error page?

推荐答案

让我们解构有趣的部分(我删除了不相关的代码):

Let's deconstruct the interesting bits (I removed irrelevant code):

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

第一个是IsDevelopment(). CLR如何知道?嗯...在Visual Studio中执行F5时会设置一个名为ASPNET_ENV的环境变量.它会自动将其设置为Development,然后会向您显示一个非常漂亮的堆栈跟踪,就像以前的<customErrors mode="Off"/>一样.

The first one is IsDevelopment(). How does the CLR knows? Well... there's an environment variable named ASPNET_ENV that is set when you do F5 in Visual Studio. It will automatically set it to Development and it will then show you a very nice stack trace just like <customErrors mode="Off"/> used to do.

如果您说的是从基本命令行运行此命令会发生什么?好吧,默认值为Production(如果需要,也可以为!IsDevelopment()),突然会使用UseExceptionHandler(...).

What happens if you are running this from a basic command line you say? Well the default value will be Production (or !IsDevelopment() if you wish) and suddenly UseExceptionHandler(...) will be used.

正是在这个时候重定向发生.

It is at that moment that the redirection will happens.

Visual Studio将自动以Development模式运行您的应用程序.

Visual Studio will run your application in Development mode automatically.

独立运行应用程序将默认为Production.

Running the application independently will default to Production.

您可以通过在环境变量中设置ASPNET_ENV来重新定义该值.它可以是任何东西,唯一会影响此代码的值是Development.

You can redefine this value by setting ASPNET_ENV in your environment variables. It can be anything and the only value that will affect this code is Development.

这篇关于在ASP.NET 5应用程序上未点击错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆