gpt4 book ai didi

c# - ASP.NET MVC 6 (ASP.NET 5) 中的 Application_PreSendRequestHeaders 和 Application_BeginRequest

转载 作者:行者123 更新时间:2023-11-30 14:51:28 26 4
gpt4 key购买 nike

如何在 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);
}

谢谢!

最佳答案

中间件!

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 .

关于c# - ASP.NET MVC 6 (ASP.NET 5) 中的 Application_PreSendRequestHeaders 和 Application_BeginRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34287959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com