Skip to content

Commit

Permalink
添加异常处理代码。
Browse files Browse the repository at this point in the history
  • Loading branch information
ltm0203 committed Jul 31, 2019
1 parent eb3c5d6 commit ff9ecc9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -37,5 +38,23 @@ public IActionResult HttpStatusCodeHandler(int statusCode)

}


[AllowAnonymous]
[Route("Error")]
public IActionResult Error()
{

var exceptionHandlerPathFeature= HttpContext.Features.Get<IExceptionHandlerPathFeature>();

ViewBag.ExceptionPath = exceptionHandlerPathFeature.Path;
ViewBag.ExceptionMessage = exceptionHandlerPathFeature.Error.Message;
ViewBag.StackTrace = exceptionHandlerPathFeature.Error.StackTrace;


return View("Error");

}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ public IActionResult Index()



public IActionResult Details(int id) {
public IActionResult Details(int id) {

throw new Exception("此异常发生在Details视图中");


Student student = _studentRepository.GetStudent(id);

if (student==null)
{
Response.StatusCode = 404;

return View("StudentNotFound",id);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
//"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_ENVIRONMENT": "Staging",
// "ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_ENVIRONMENT": "Production",
"MyKey": "appsetting.launchSetting.json MyKey "
},

Expand Down
7 changes: 4 additions & 3 deletions StudentManagement/StudentManagement/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ public void ConfigureServices(IServiceCollection services) {
// This method gets called by the runtim0e. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//如果环境是Development,调用 Developer Exception Page
//如果环境是 Development,调用 Developer Exception Page
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePagesWithReExecute("/Error/{0}");
// app.UseStatusCodePagesWithRedirects("/Error/{0}");
app.UseExceptionHandler("/Error");//拦截我们的异常
app.UseStatusCodePagesWithReExecute("/Error/{0}"); //拦截404找不到的页面信息

}


Expand Down
48 changes: 48 additions & 0 deletions StudentManagement/StudentManagement/Views/Error/Error.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
@*
For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{

ViewBag.Title = "异常错误页面";

}



<h3>程序请求时,发生了一个内部错误,我们会反馈给开发团队,尽快解决这个问题。</h3>



<h5>请听过 ltm@ddxc.org 与我们取得联系</h5>

<hr />

<h3>错误详情:</h3>


<div class="alert alert-danger">
<h5>异常路径:</h5>

<hr />
<p>@ViewBag.ExceptionPath</p>

</div>



<div class="alert alert-danger">

<h5>异常内容:</h5>

<hr />
<p>@ViewBag.ExceptionMessage</p>
</div>


<div class="alert alert-danger">
<h5>异常堆栈跟踪:</h5>

<hr />
<p>@ViewBag.StackTrace</p>

</div>

0 comments on commit ff9ecc9

Please sign in to comment.