ASP.NET MVC 6(ASP.NET 5)中的Application_PreSendRequestHeaders和Application_BeginRequest [英] Application_PreSendRequestHeaders and Application_BeginRequest in ASP.NET MVC 6 (ASP.NET 5)

查看:39
本文介绍了ASP.NET MVC 6(ASP.NET 5)中的Application_PreSendRequestHeaders和Application_BeginRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ASP.NET 5(MVC6)中使用这些方法.在MVC5中,我在Global.asax中使用了它……现在呢?也许是初创班吗?

How can I use these methods in ASP.NET 5 (MVC6). In MVC5, I used it in Global.asax... and now? Startup class maybe?

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            app?.Context?.Response.Headers.Remove("Server");
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (this.Request.Url.Host.StartsWith("www") || this.Request.Url.IsLoopback) return;
            var url = new UriBuilder(this.Request.Url) { Host = "www." + this.Request.Url.Host };
            this.Response.RedirectPermanent(url.ToString(), endResponse: true);
        }

谢谢!

推荐答案

中间件!

public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
{
    app.Use(next => async context =>
    {
        context.Response.Headers.Remove("Server");
        await next.Invoke(context);
    });

    app.Use(next => async context => {
        if (context.Request.Path.ToString().StartsWith("www"))
            await next.Invoke(context);
        else
            context.Response.Redirect("www" + context.Request.Path.ToString());
    });
}

这是一个很好的教程.

这篇关于ASP.NET MVC 6(ASP.NET 5)中的Application_PreSendRequestHeaders和Application_BeginRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆