ASP.Net 5 MVC 6,如何使用共享的Error.cshtml作为默认错误响应

agu*_*ars 11 c# asp.net-mvc razor asp.net-core

ASP.Net 5 MVC 6,如何使用共享的Error.cshtml作为默认错误响应

使用带有剃刀视图的Microsoft.AspNet.Diagnostics UseExceptionHandler中间件时

如果您查看https://github.com/aspnet/Diagnostics/tree/dev/samples/ExceptionHandlerSample/Startup.cs中的示例代码, 解释如何使用Microsoft.AspNet.Diagnostics ErrorHandler中间件进行ASP.Net 5,评论说:

//通常你会使用MVC或类似的东西来渲染一个漂亮的页面.

好的,但怎么做?

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        // Configure the error handler to show an error page.
        app.UseExceptionHandler(errorApp =>
        {
            // Normally you'd use MVC or similar to render a nice page.
            errorApp.Run(async context =>
            {
Run Code Online (Sandbox Code Playgroud)

agu*_*ars 28

在Startup类中:

app.UseExceptionHandler("/Home/Error");
Run Code Online (Sandbox Code Playgroud)

在HomeController中:

public IActionResult Error()
{
    var feature = this.HttpContext.Features.Get<IExceptionHandlerFeature>();
    return View("~/Views/Shared/Error.cshtml", feature?.Error);
}
Run Code Online (Sandbox Code Playgroud)

Error.cshtml视图可能如下所示:

@model Exception

@{
    ViewBag.Title = "Oops!";
}
<h1 class="text-danger">Oops! an error occurs</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>
@if (Model != null)
{
    @Html.ValueFor(model => model.Message)
}
Run Code Online (Sandbox Code Playgroud)

此代码是GitHub上可用项目的一部分


Chr*_*ald 8

要处理404和内部错误,您需要修改错误签名.

我在Startup.cs中的Dev环境中明确地注释掉了调试错误处理程序.如果您不想这样做,请在项目中使用环境变量.

将其添加到Startup.cs

    if (env.IsDevelopment())
    {
        // Uncomment when done testing error handling
        //app.UseBrowserLink();
        //app.UseDeveloperExceptionPage();
        //app.UseDatabaseErrorPage();

        // Comment when done testing error handling
        app.UseExceptionHandler("/Home/Error");
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");

        //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<ApplicationDbContext>()
                    .Database.Migrate();
            }
        }
        catch { }
    }


    // Lines Skipped For Brevity ....


    // Add this line above app.Mvc in Startup.cs to Handle 404s etc
    app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
Run Code Online (Sandbox Code Playgroud)

将其添加到HomeController.cs

    using Microsoft.AspNet.Mvc;
    using Microsoft.AspNet.Diagnostics;
    using Microsoft.AspNet.Http.Features;

    // id = Http Status Error
    public IActionResult Error(String id)
    {
        var feature = HttpContext.Features.Get<IExceptionHandlerFeature>();

        var undhandledException = feature?.Error;
        var iisError = id;

        return View();
    }
Run Code Online (Sandbox Code Playgroud)