2015-07-20 89 views
5

應用程序事件如果我要處理應用程序事件在我的ASP.NET應用程序,我會在我的Global.asax註冊處理程序:註冊處理程序在ASP.NET 5

protected void Application_BeginRequest(object sender, EventArgs e) 
{ ... } 

Global.asax已經從ASP刪除。 NET 5.我現在如何處理這些事件?

+0

從我所能找到的,我相信這一切都在ASP.NET 5的'startup.cs'文件中完成。http://wildermuth.com/2015/3/2/A_Look_at_ASP_NET_5_Part_2_-_Startup –

+0

@德魯肯尼迪 - 嘿嘿你更快,甚至提供相同的鏈接 – 2015-07-20 13:09:32

+0

@ Tanis83是!我會提供它作爲答案,但實際上這只是一個猜測。 :p –

回答

-1

ASP.NET應用程序可以在沒有global.asax的情況下運行。

HTTPModule是global.asax的替代方案。

閱讀更多here

1

在ASP.NET 5中爲每個請求運行一些邏輯的方法是通過中間件。下面是一個簡單的中間件:

public class FooMiddleware 
{ 
    private readonly RequestDelegate _next; 

    public FooMiddleware(RequestDelegate next) 
    { 
     _next = next; 
    } 

    public async Task Invoke(HttpContext context) 
    { 
     // this will run per each request 
     // do your stuff and call next middleware inside the chain. 

     return _next.Invoke(context); 
    } 
} 

然後,您可以在您的Startup類註冊此:

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

在這裏看到more information on Middlewares in ASP.NET 5

對於任何應用程序開始級調用,請參閱application startup documentation

+0

我是否也將中間件用於所有其他事件? 'Application_Start','Application_AuthenteicateRequest','Session_Start'等等......? –

+0

中間件的調用方法將根據每個請求調用,具體取決於中間件位於鏈中的位置。對於應用程序啓動,在'Startup'方法中有幾個地方可以像'Startup''','ConfigureServices'方法和'Configure'方法一樣標籤。 – tugberk

+0

在這裏查看關於'Startup'類的更多信息:http://docs.asp.net/en/latest/conceptual-overview/understanding-aspnet5-apps.html#application-startup – tugberk

相關問題