Application_PreSendRequestHeaders и Application_BeginRequest в ASP.NET MVC 6 (ASP.NET 5)

Как я могу использовать эти методы в 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);
        }

Спасибо!

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

Вот хороший учебник.

Другие вопросы по тегам