gpt4 book ai didi

asp.net - 通过 responseMode ="ExecuteURL"清除 HttpContext.Current.Items ?

转载 作者:行者123 更新时间:2023-12-04 21:42:39 24 4
gpt4 key购买 nike

我避免使用默认的 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.Itemshttp://weblog.west-wind.com/posts/2010/Jan/20/HttpContextItems-and-ServerTransferExecute )

但我也在 Items 中捕捉到了一些东西在“ExecuteURL”之前,并在传输(或其他任何内容)之后检索/输出它,它似乎消失了。我看过它进入 Items收藏,我看到了 Count提高到 5,然后当检索到该值时,集合中只有 2 个项目。

到底是怎么回事?

如果您想更多地了解我在做什么并推荐替代实现,我愿意接受。我正在使用它以一种不受竞争条件影响的方式将 ELMAH 错误 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 在请求之间被清除,您的代码总是看到生成的第二个请求,因此项目之间的差异。

尝试帖子中的建议:使用 system.web > customErrors with redirectMode="ResponseRewrite"代替。

关于asp.net - 通过 responseMode ="ExecuteURL"清除 HttpContext.Current.Items ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22663322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com