加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 通过responseMode =“ExecuteURL”清除HttpContext.C

发布时间:2020-12-16 03:16:20 所属栏目:asp.Net 来源:网络整理
导读:我避免使用默认的ASP.NET方法来重定向错误(就像许多人一样).清洁 AJAX代码和SEO是原因之一. 但是,我使用以下方法来执行此操作,似乎我可能会在传输中丢失HttpContext.Current.Items? httpErrors errorMode="Custom" existingResponse="Replace" remove statu
我避免使用默认的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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读