asp.net – 通过responseMode =“ExecuteURL”清除HttpContext.C
我避免使用默认的ASP.NET方法来重定向错误(就像许多人一样).清洁
AJAX代码和SEO是原因之一.
但是,我使用以下方法来执行此操作,似乎我可能会在传输中丢失HttpContext.Current.Items? <httpErrors errorMode="Custom" existingResponse="Replace"> <remove statusCode="401" /> <remove statusCode="403" /> <remove statusCode="404" /> <remove statusCode="500" /> <error statusCode="401" responseMode="ExecuteURL" path="/Account/SignIn" /> <error statusCode="403" responseMode="ExecuteURL" path="/Site/Forbidden" /> <error statusCode="404" responseMode="ExecuteURL" path="/Site/NotFound" /> <error statusCode="500" responseMode="ExecuteURL" path="/Site/Error" /> </httpErrors> 我假设它只是在封面下执行了一个Server.Transfer(),据我所知,它保留了Items. (见:Scope of HttpContext.Current.Items和http://weblog.west-wind.com/posts/2010/Jan/20/HttpContextItems-and-ServerTransferExecute) 但是我也在“ExecuteURL”之前在Items中捕获了一些东西,并在转移之后检索/输出它(或者不管它是什么),它似乎消失了.我看过它进入Items集合,我看到Count提升到5,然后当检索到值时,集合中只有2个项目. 到底是怎么回事? 如果你想更多地了解我正在做的事情并推荐一个替代实现,我愿意接受它.我正在使用它以一种没有竞争条件的方式将ELMAH Error Id推送到ViewModel中. (即我正在替换的常见解决方法是仅显示最近的错误.)这是我的代码: Global.asax中 protected void ErrorLog_Logged(object sender,ErrorLoggedEventArgs args) { ElmahSupplement.CurrentId = args.Entry.Id; } void ErrorLog_Filtering(object sender,ExceptionFilterEventArgs e) { if (ElmahSupplement.IsNotFound(e.Exception)) { ElmahSupplement.LogNotFound((e.Context as HttpContext).Request); e.Dismiss(); } } SiteController.cs public virtual ActionResult Error() { Response.StatusCode = 500; return View(MVC.Site.Views.Error,ElmahSupplement.CurrentId); } ElmahSupplement.cs public class ElmahSupplement { // TODO: This is a rather fragile way to access this info private static readonly Guid contextId = new Guid("A41A67AA-8966-4205-B6C1-14128A653F21"); public static string CurrentId { get { return // Elmah 1.2 will fail to log when enumerating form values that raise RequestValidationException (angle brackets) // https://code.google.com/p/elmah/issues/detail?id=217 // So this id could technically be empty here (HttpContext.Current.Items[contextId] as string); } set { HttpContext.Current.Items[contextId] = value; } } public static void LogNotFound(HttpRequest request) { var context = RepositoryProxy.Context; context.NotFoundErrors.Add(new NotFoundError { RecordedOn = DateTime.UtcNow,Url = request.Url.ToString(),ClientAddress = request.UserHostAddress,Referrer = request.UrlReferrer == null ? "" : request.UrlReferrer.ToString() }); context.SaveChanges(); } public static bool IsNotFound(Exception e) { HttpException he = e as HttpException; return he != null && he.GetHttpCode() == 404; } } 解决方法
如
here所述,ExecuteURL生成两个请求:第一个抛出异常,第二个生成错误响应.
由于Context.Items在请求之间被清除,因此您的代码始终会看到生成的第二个请求,因此Items之间存在差异. 在帖子中尝试消化:使用system.web>使用redirectMode =“ResponseRewrite”的customErrors. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 用破折号路由值
- asp.net-mvc – 如何创建特定于区域,控制器和操作的自定义A
- asp.net-mvc-4 – ASP .Net MVC 4 Authorize和AllowAnonymo
- ASP.Net ListView按数据字段分组?
- asp.net-mvc – ASP.NET MVC – 在客户端缓存页面
- asp.net-mvc-4 – TempData不适用于MVC4中的第二个请求
- asp.net – MVC标识||禁用所有cookie并“记住我”选项?
- ASP.NET MVC如何连接到ASP.NET?
- ASP.NET MVC是否使Web表单成为旧版平台?
- asp.net – 加快你的IIS