我想要这个:
/Pages/401.cshtml/Pages/403.cshtml/Pages/404.cshtml
/Pages/400_599.cshtml有可能吗?
当然可以,只需在中创建页面
Pages
文件夹如下:
然后使用
app.UseStatusCodePagesWithReExecute("/{0}")
在Program.cs中:
app.UseStatusCodePagesWithReExecute("/{0}");
app.UseExceptionHandler("/500");
//app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
对于包罗万象的页面
,您可以获得如下错误页面:
@page "/Error/{statusCode}"
@model ErrorModel
@{
var statusCode = HttpContext.Request.RouteValues["statusCode"];
}
@switch (statusCode)
{
case "400":
<h1>BadRequest</h1>
<p>You send wrong model to this resource.</p>
break;
case "401":
<h1>Unauthorized</h1>
<p>You are not authorized to this resource.</p>
break;
case "403":
<h1>Forbidden</h1>
<p>You don't have permission to this resource.</p>
break;
case "404":
<h1>Not Found</h1>
<p>The resource you are looking for could not be found.</p>
break;
default:
<h1>Error @(statusCode)</h1>
<p>An error occurred while processing your request.</p>
break;
}
程序.cs:
app.UseStatusCodePagesWithReExecute("/Error/{0}");
app.UseExceptionHandler("/Error/500");