【问题标题】:How to implement global.asax events by OWIN如何通过 OWIN 实现 global.asax 事件
【发布时间】:2016-03-11 06:51:35
【问题描述】:

从 asp.net 6 开始不会有名为 global.asax 的文件,但 global.asax 有很多类似的事件

· Application_Init

· Application_Start

· Session_Start

· Application_BeginRequest

· Application_EndRequest

· Application_AuthenticateRequest

· Application_Error

· Session_End

· Application_End

比如说我经常使用Application_BeginRequest 事件来重定向用户。这是一个示例代码

protected void Application_BeginRequest(Object sender, EventArgs e)
{
            if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("content.aspx?content=gm-mdi-diagnostic-tool"))
            {
                Response.RedirectPermanent("http://shop.bba-reman.com/product-category/diagnostic-tools/oem-diagnostic-tools/", true);
            }
            else if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("content.aspx?content=landrover_rover_t4_testbook_lite_diagnostic_tool_ids"))
            {
                Response.RedirectPermanent("http://shop.bba-reman.com/shop/oem-diagnostic-tools/land-rover-t4-mobile/", true);
            }
            else if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("content.aspx?content=ford_ids_main_dealer_tool_mazda_jaguar_landrover"))
            {
                Response.RedirectPermanent("http://shop.bba-reman.com/product-category/diagnostic-tools/oem-diagnostic-tools/", true);
            }
            else if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("content=coda_fuelling_tester_dynamically_measure_fuel_flow_and_pressure_in_situ_under_load"))
            {
                Response.RedirectPermanent("http://shop.bba-reman.com/product-category/diagnostic-tools/", true);
            }
}            

那么告诉我如何对 OWIN 做同样的事情?与代码示例讨论。

还告诉我如何从OWIN class code 捕获session start / end or application start or end

请讨论谢谢

【问题讨论】:

  • 讨论代码示例通常是一个糟糕的问题类型 - 您应该先分享您尝试过的内容。你会为这类事情编写过滤器。
  • 我想知道如何用 owin 实现 Application_BeginRequest .....有什么想法吗?

标签: owin asp.net-core-mvc


【解决方案1】:

如果您需要在每次请求之前执行一些自定义,我建议您使用标准方式:创建一个中间件并将其插入到 HTTP 请求管道中。

创建中间件的方法有很多,但是为了清晰和可维护性,创建一个新的类是一个不错的选择。这是一个例子:

public class MyMiddleware
{
  private readonly RequestDelegate next;

  public MyMiddleware(RequestDelegate next)
  {
    this.next = next;
  }

  public async Task Invoke(HttpContext context)
  {
    this.BeginInvoke(context);
    await this.next.Invoke(context);
    this.EndInvoke(context);
  }

  private void BeginInvoke(HttpContext context)
  {
    // Do custom work before controller execution
  }

  private void EndInvoke(HttpContext context)
  {
    // Do custom work after controller execution
  }
}

接下来只剩下注册中间件了。这是在 Startup 类的 Configure 方法中完成的:

public class Startup
{
  public void Configure(IApplicationBuilder app)
  {
    ...
    app.UseMiddleware<MyMiddleware>();
    ...
  }
}

就是这样。忘记 global.asax ;-)

【讨论】:

  • 有什么方法可以用owin Middle ware来处理会话的开始和结束……如果可能的话,你可以和代码讨论一下。
  • Global.asax 仍然需要用于 OWIN 中无法处理的许多事件,例如会话事件和非 OWIN 异常处理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
相关资源
最近更新 更多