【问题标题】:Application_PreSendRequestHeaders and Application_BeginRequest in ASP.NET MVC 6 (ASP.NET 5)ASP.NET MVC 6 (ASP.NET 5) 中的 Application_PreSendRequestHeaders 和 Application_BeginRequest
【发布时间】:2015-12-15 11:24:04
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: c# asp.net-core asp.net-core-mvc


    【解决方案1】:

    中间件!

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

    这是一个很好的tutorial

    【讨论】:

    • 谢谢兄弟!!现在就像 Laravel 一样!! ;)
    猜你喜欢
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 1970-01-01
    • 2016-03-29
    • 2016-05-11
    相关资源
    最近更新 更多