Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

asp.net core mvc - How to implement global.asax events by OWIN

from asp.net 6 there would be no file called global.asax but global.asax has many events like

· Application_Init

· Application_Start

· Session_Start

· Application_BeginRequest

· Application_EndRequest

· Application_AuthenticateRequest

· Application_Error

· Session_End

· Application_End

say for example i often work with Application_BeginRequest event to redirect user. here is a sample code

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);
            }
}            

so tell me how to do the same with OWIN ? discuss with code sample.

also tell me how to capture session start / end or application start or end from OWIN class code ?

please discuss thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you need to execute some custom before each request, I recommend you to use the standard way : create a middleware and insert it in the HTTP request pipeline.

There is many ways to create a middleware, but for clarity and maintainability, create a new class is a good choice. Here is an exemple :

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
  }
}

Next it only remains to register the middleware. This is done in the Configure method of Startup class :

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

That's all. And forget global.asax ;-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...