重构代码:合并AccessDenied页面与Error页面逻辑 link #ISHLV
#commit commit:b6b90e3
This commit is contained in:
parent
e47bcbb99b
commit
99b8713ae0
|
@ -84,11 +84,6 @@ namespace Bootstrap.Admin.Controllers
|
|||
/// </summary>
|
||||
/// <returns>The denied.</returns>
|
||||
[ResponseCache(Duration = 600)]
|
||||
public ActionResult AccessDenied()
|
||||
{
|
||||
var returnUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].ToString();
|
||||
ViewBag.ReturnUrl = string.IsNullOrEmpty(returnUrl) ? Url.Content("~/Home/Index") : returnUrl;
|
||||
return View();
|
||||
}
|
||||
public ActionResult AccessDenied() => View("Error", ErrorModel.CreateById(403));
|
||||
}
|
||||
}
|
|
@ -30,27 +30,12 @@ namespace Bootstrap.Admin.Controllers
|
|||
[AllowAnonymous]
|
||||
public IActionResult Error(int id)
|
||||
{
|
||||
var returnUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].ToString();
|
||||
var model = new ErrorModel() { ReturnUrl = string.IsNullOrEmpty(returnUrl) ? Url.Content("~/Home/Index") : returnUrl };
|
||||
model.Title = "服务器内部错误";
|
||||
model.Content = "服务器内部错误";
|
||||
model.Image = "error_icon.png";
|
||||
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");
|
||||
}
|
||||
var model = ErrorModel.CreateById(id);
|
||||
if (id != 403)
|
||||
{
|
||||
var returnUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].ToString();
|
||||
if (!string.IsNullOrEmpty(returnUrl)) model.ReturnUrl = returnUrl;
|
||||
}
|
||||
return View(model);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,11 @@
|
|||
/// </summary>
|
||||
public class ErrorModel
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
@ -20,9 +25,51 @@
|
|||
/// </summary>
|
||||
public string Image { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
public string Detail { get; set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
Loading…
Reference in New Issue