重构代码:合并AccessDenied页面与Error页面逻辑 link #ISHLV

#commit commit:b6b90e3
This commit is contained in:
Argo Zhang 2019-03-07 01:12:36 +08:00
parent e47bcbb99b
commit 99b8713ae0
6 changed files with 79 additions and 66 deletions

View File

@ -84,11 +84,6 @@ namespace Bootstrap.Admin.Controllers
/// </summary> /// </summary>
/// <returns>The denied.</returns> /// <returns>The denied.</returns>
[ResponseCache(Duration = 600)] [ResponseCache(Duration = 600)]
public ActionResult AccessDenied() public ActionResult AccessDenied() => View("Error", ErrorModel.CreateById(403));
{
var returnUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].ToString();
ViewBag.ReturnUrl = string.IsNullOrEmpty(returnUrl) ? Url.Content("~/Home/Index") : returnUrl;
return View();
}
} }
} }

View File

@ -30,27 +30,12 @@ namespace Bootstrap.Admin.Controllers
[AllowAnonymous] [AllowAnonymous]
public IActionResult Error(int id) public IActionResult Error(int id)
{ {
var returnUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].ToString(); var model = ErrorModel.CreateById(id);
var model = new ErrorModel() { ReturnUrl = string.IsNullOrEmpty(returnUrl) ? Url.Content("~/Home/Index") : returnUrl }; if (id != 403)
model.Title = "服务器内部错误"; {
model.Content = "服务器内部错误"; var returnUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].ToString();
model.Image = "error_icon.png"; if (!string.IsNullOrEmpty(returnUrl)) model.ReturnUrl = returnUrl;
if (id == 0) }
{
model.Content = "未处理服务器内部错误";
}
else if (id == 404)
{
model.Title = "资源未找到";
model.Content = "请求资源未找到";
model.Image = "404_icon.png";
}
else if (id == 403)
{
model.Title = "拒绝响应";
model.Content = "请求资源的访问被服务器拒绝";
model.ReturnUrl = Url.Content("~/Admin/Index");
}
return View(model); return View(model);
} }
} }

View File

@ -5,6 +5,11 @@
/// </summary> /// </summary>
public class ErrorModel public class ErrorModel
{ {
/// <summary>
///
/// </summary>
public int Id { get; set; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -20,9 +25,51 @@
/// </summary> /// </summary>
public string Image { get; set; } public string Image { get; set; }
/// <summary>
///
/// </summary>
public string Detail { get; set; }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
public string ReturnUrl { get; set; } public string ReturnUrl { get; set; }
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static ErrorModel CreateById(int id)
{
var model = new ErrorModel
{
Id = id,
Title = "服务器内部错误",
Content = "服务器内部错误",
Detail = "相关错误信息已经记录到日志中,请登录服务器或后台管理中查看",
Image = "~/Images/error_icon.png",
ReturnUrl = "~/Admin/Index"
};
switch (id)
{
case 0:
model.Content = "未处理服务器内部错误";
break;
case 404:
model.Title = "资源未找到";
model.Content = "请求资源未找到";
model.Image = "~/Images/404_icon.png";
break;
case 403:
model.Title = "未授权请求";
model.Content = "您的访问受限!";
model.Detail = "服务器拒绝处理您的请求!您可能没有访问此操作的权限";
break;
}
return model;
}
} }
} }

View File

@ -1,21 +0,0 @@
@{
ViewBag.Title = "未授权请求";
Layout = "_Layout";
}
@section css {
<link href="~/css/error.css" rel="stylesheet" asp-append-version="true" />
}
@section javascript {
<script src="~/js/error.js" asp-append-version="true"></script>
}
<section class="error-wrapper">
<img src="~/images/error_icon.png" />
<h1>您的访问受限!</h1>
<h3>服务器拒绝处理您的请求!您可能没有访问此操作的权限</h3>
<p><a href="~/Account/Logout">登录</a> <span>或者</span> <a href="~/Home/Index" target="_top">返回首页</a></p>
<div>
<p>也可能是以下原因导致您没有权限</p>
<p>1. 没有登录,请登录后查看</p>
<p>2. 当前用户未获得此资源的相应权限,请联系管理员</p>
</div>
</section>

View File

@ -1,18 +0,0 @@
@model ErrorModel
@{
ViewBag.Title = Model.Title;
Layout = "_Layout";
}
@section css {
<link href="~/css/error.css" rel="stylesheet" />
}
@section javascript {
<script src="~/js/error.js"></script>
}
<section class="error-wrapper">
<img src="~/images/@Model.Image" />
<h1>@Model.Content</h1>
<h3>相关错误信息已经记录到日志中,请登录服务器查看</h3>
<br />
<a href="@Model.ReturnUrl" target="_top">返回首页</a>
</section>

View File

@ -0,0 +1,25 @@
@model ErrorModel
@{
ViewBag.Title = Model.Title;
Layout = "_Layout";
}
@section css {
<link href="~/css/error.css" rel="stylesheet" asp-append-version="true" />
}
@section javascript {
<script src="~/js/error.js" asp-append-version="true"></script>
}
<section class="error-wrapper">
<img src="@Url.Content(Model.Image)" />
<h1>@Model.Content</h1>
<h3>@Model.Detail</h3>
<p><a href="~/Account/Logout">登录</a> <span>或者</span> <a href="@Url.Content(Model.ReturnUrl)">返回首页</a></p>
@if (Model.Id == 403)
{
<div>
<p>也可能是以下原因导致您没有权限</p>
<p>1. 没有登录,请登录后查看</p>
<p>2. 当前用户未获得此资源的相应权限,请联系管理员</p>
</div>
}
</section>