2015-12-15 119 views
2

如何在ASP.NET 5(MVC6)中使用這些方法。在MVC5中,我在Global.asax中使用了它,現在呢?啓動課也許?ASP.NET MVC 6中的Application_PreSendRequestHeaders和Application_BeginRequest(ASP.NET 5)

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

謝謝!

回答

4

中間件!

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

+0

謝謝兄弟!現在就像Laravel! ;) – chemitaxis