Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET 5中注册应用程序事件的处理程序_C#_Asp.net_Visual Studio_Asp.net Core - Fatal编程技术网

C# 在ASP.NET 5中注册应用程序事件的处理程序

C# 在ASP.NET 5中注册应用程序事件的处理程序,c#,asp.net,visual-studio,asp.net-core,C#,Asp.net,Visual Studio,Asp.net Core,如果我想在我的ASP.NET应用程序中处理应用程序事件,我会在我的Global.asax中注册一个处理程序: protected void Application_BeginRequest(object sender, EventArgs e) { ... } Global.asax已从ASP.NET 5中删除。现在如何处理此类事件?ASP.NET应用程序可以在没有global.asax的情况下运行 HTTPModule是global.asax的替代品 阅读更多。在ASP.NET 5中,为每个

如果我想在我的ASP.NET应用程序中处理应用程序事件,我会在我的
Global.asax
中注册一个处理程序:

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

Global.asax
已从ASP.NET 5中删除。现在如何处理此类事件?

ASP.NET应用程序可以在没有global.asax的情况下运行

HTTPModule是global.asax的替代品


阅读更多。

在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);
    }
}
然后,您可以在
启动
类中注册:

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.UseMiddleware<FooMiddleware>();
    }
}
公共类启动
{
公共void配置(IApplicationBuilder应用程序)
{
app.UseMiddleware();
}
}
请看这里


对于任何应用程序启动级调用,请参见。

根据我所能找到的,我相信这都是在ASP.NET 5中的
startup.cs
文件中完成的@Drew Kennedy-嘿嘿,你跑得更快,甚至提供了同样的服务link@Tanis83是的!我本想提供一个答案,但实际上这只是一个猜测p我是否也可以对所有其他事件使用中间件<代码>应用程序启动,
应用程序验证请求
会话启动
,等等?中间件的调用方法将根据每个请求调用,具体取决于中间件在链中的位置。对于应用程序启动,在
Startup
方法中有几个地方,您可以在其中使用选项卡,如
Startup
ctor,
ConfigureServices
方法和
Configure
方法。在这里查看有关
Startup
类的更多信息:@tugberk这是否意味着ASP.Net MVC项目中的Startup类将取代ASP.Net Web表单中的全局文件?