【问题标题】:How to access HttpContext.Current.Application on OWIN Startup如何在 OWIN 启动时访问 HttpContext.Current.Application
【发布时间】:2016-09-07 13:17:46
【问题描述】:

我需要在 OWIN 启动时填写 HttpContext.Current.Application["Translations"],但 HttpContext.Currentnull

我应该在Global.asax.cs 中使用Application_Start() 还是不好的主意?

我有什么选择?

【问题讨论】:

  • HttpContext 在启动时不可用,因为它在没有请求的情况下无法存在。 Begin_Request

标签: asp.net-web-api owin httpcontext


【解决方案1】:

HttpContext 在启动时不可用,因为没有请求就无法存在。 Startup 类只为应用程序运行一次,而不是为每个请求运行一次。

尝试在Global.asax.csBeginRequest 处理程序中访问它。

protected void Application_BeginRequest(object sender, EventArgs e) {

    base.Application["Translations"] = "My value here";

}

【讨论】:

  • 我在使用 OWIN 时可以使用Global.asax.cs 吗?我曾经说过要删除它的指南。
  • 如果不是自托管的,应该没有问题。如果您使用的是 Global.asax,则假设它在 IIS 中托管。两者可以共存
  • 谢谢,我会按照你的建议尝试。
  • 对 Owin 保持纯粹的更好方法是编写中间件来处理这个问题。
  • @RonDeFreitas,这听起来很有趣。我没有想到这一点。如何通过中间件来做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多