Sam
Sam

Reputation: 30288

Not hitting error page on ASP.NET 5 app

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.

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.

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?

Upvotes: 2

Views: 826

Answers (3)

Mike Hughes
Mike Hughes

Reputation: 77

In no way is this the recommended way to achieve this but you could use:

     if (app.WebRootPath.Contains("Development"))
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
             app.UseExceptionHandler("/Home/Error");
        }

This checks the file path where you have stored your web application for the word 'Development' and applies the settings accordingly, you could use method to force an environment change (as long as your web root is set up correctly).

More more info, look at this link:

https://blogs.msdn.microsoft.com/webdev/2016/02/01/an-update-on-asp-net-core-and-net-core/ and this: http://docs.asp.net/en/latest/fundamentals/environments.html

RC2 is changing some fundamental things about deployment and hosting, so it might be worth reading up on them now before you get yourself to far down a path?

Upvotes: 0

Maxime Rouiller
Maxime Rouiller

Reputation: 13699

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

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

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.

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.

Conclusion

Visual Studio will run your application in Development mode automatically.

Running the application independently will default to Production.

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.

Upvotes: 3

Stafford Williams
Stafford Williams

Reputation: 9806

It sounds like you are executing this via DNX via cmd, which will set ASPNET_ENV to Production, rather than Development.

I like to log the environment name on startup to help debug this type of scenario;

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    loggerFactory.CreateLogger("Startup")                     // add
        .LogInformation($"Startup: {env.EnvironmentName}");   // this

    ...
}

Otherwise you could stick a breakpoint on if (env.IsDevelopment()) to make sure you're hitting it. If you run the project in VS2015, it will run as Development, however, if you run it via DNX on the command prompt, you'll need to set the ASPNET_ENV environment variable to Development.

Upvotes: 0

Related Questions