- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
对于标准错误,我使用解决方案错误句柄(见下面的代码),我也尝试了 THIS (无效)
Global.asax.cs
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
var httpException = exception as HttpException;
var routeData = new RouteData();
routeData.Values.Add("controller", "Error");
if (httpException == null)
routeData.Values.Add("action", "Index");
else //It's an Http Exception, Let's handle it.
switch (httpException.GetHttpCode())
{
case 404:
// Page not found.
routeData.Values.Add("action", "HttpError404");
break;
case 500:
// Server error.
routeData.Values.Add("action", "HttpError500");
break;
// Here you can handle Views to other error codes.
// I choose a General error template
default:
routeData.Values.Add("action", "General");
break;
}
// Pass exception details to the target error View.
routeData.Values.Add("error", exception);
var request = Request;
// Pass request details to the target request View.
routeData.Values.Add("request", request);
// Clear the error on server.
Server.ClearError();
// Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(
new HttpContextWrapper(Context), routeData));
}
错误 Controller
public class ErrorController : Controller
{
private readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
// GET: Error
public ActionResult Index()
{
return View();
}
public ActionResult HttpError404(string error, HttpRequest request)
{
ViewBag.Title = "Page not found (Error 404)";
ViewBag.Description = error;
return View("~/Views/Shared/Error.cshtml");
}
public ActionResult HttpError500(string error, HttpRequest request)
{
ViewBag.Title = "Internal server error (Error 500)";
ViewBag.Description = error;
return View("~/Views/Shared/Error.cshtml");
}
public ActionResult General(string error, HttpRequest request)
{
ViewBag.Title = "Internal server error";
ViewBag.Description = error;
return View("~/Views/Shared/Error.cshtml");
}
}
Error.cshtml
@{
Layout = "_Layout.cshtml";
}
<div class="row">
<div class="exceptionmessage col-md-12 text-center">
<div class="center-block">
<img src="~/Content/i/404.png" class="img-responsive"/>
</div>
<strong>@ViewBag.Message</strong>
<span class="center-block small text-uppercase">@ViewBag.Main</span>
<div class="center-block">@Html.ActionLink("Return to the Homepage", "Index", "Home", null, new {@class = "btn btn-primary"})</div>
</div>
</div>
我有一个目录 Techs
,其中有一些 XML 文件(用于加载数据,比如本地数据库),并且在 web.config
中使用这个脚本我禁用了访问到这个文件夹并从这个文件夹下载文件(或者当我写不存在的 URL 或类似文件 www.test.com/afsdfgsdfg.pdf
)时,我收到 IIS 消息 404 Page not found without redirection to我的自定义错误 404 页面来自下面的错误处理程序代码。
<system.web>
<customErrors mode="On" defaultRedirect="~/Error">
<error redirect="~/Error/HttpError404" statusCode="404" />
</customErrors>
</system.web>
<system.webServer>
<security xdt:Transform="Replace">
<requestFiltering>
<hiddenSegments>
<add segment="Techs" />
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>
有没有办法处理所有 404 页面未找到的错误(同一页面而不是两个不同的页面)?
谢谢
编辑:
我将我的 ErrorController
的新路由添加到 RouteConfig.cs
中:
routes.MapRoute("Error",
"{controller}/{action}/",
new { controller = "Error", action = "HttpError404" });
之后,我将 web.config
更改为:
<customErrors mode="On" defaultRedirect="~/Error/General">
<error redirect="~/Error/HttpError404" statusCode="404" />
<error redirect="~/Error/HttpError500" statusCode="500" />
</customErrors>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error
statusCode="404"
path="/Error/HttpError404"
responseMode="ExecuteURL"/>
</httpErrors>
但它仍然不起作用,我收到了 HTTP Error 404.0 - Not Found 或 HTTP Error 403.14 - Forbidden 和类似的消息,当我写例如:
最佳答案
我的有效解决方案
我没有向 RouteConfig.cs 添加任何新路由,仅编辑下面的文件。我在没有参数的情况下调用方法的 Controller ,我受到了不同的对待。 Global.asax.cs 和 ErrorController 使用同上。
Web.config
<system.web>
<customErrors mode="On" defaultRedirect="~/Error/General">
<error redirect="~/Error/HttpError404" statusCode="403" />
<error redirect="~/Error/HttpError404" statusCode="404" />
<error redirect="~/Error/HttpError500" statusCode="500" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="403" />
<remove statusCode="404" />
<remove statusCode="500" />
<error
statusCode="403"
path="/Error/HttpError404"
responseMode="ExecuteURL" />
<error
statusCode="404"
path="/Error/HttpError404"
responseMode="ExecuteURL" />
<error
statusCode="500"
path="/Error/HttpError500"
responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
关于c# - ASP.NET MVC 5 - 错误句柄 - 找不到 404 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52332721/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!