Mou*_*Mou 7 c# httpmodule asp.net-core-mvc
在 asp.net 4.0 中,我们可以使用 http 模块来重写模块,如下所示:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string CountryCodeInUrl = "", redirectUrl="";
var countryCode = CookieSettings.ReadCookie();
if (countryCode=="")
{
countryCode = "gb";
}
if (HttpContext.Current.Request.RawUrl.Length >= 2)
{
CountryCodeInUrl = HttpContext.Current.Request.RawUrl.Substring(1, 2);
}
if (countryCode != CountryCodeInUrl)
{
if (HttpContext.Current.Request.RawUrl.Length >= 2)
{
if (HttpContext.Current.Request.RawUrl.Substring(1, 2) != "")
{
countryCode = HttpContext.Current.Request.RawUrl.Substring(1, 2);
}
}
if(!System.Web.HttpContext.Current.Request.RawUrl.Contains(countryCode))
{
redirectUrl = string.Format("/{0}{1}", countryCode, System.Web.HttpContext.Current.Request.RawUrl);
}
else
{
redirectUrl = System.Web.HttpContext.Current.Request.RawUrl;
}
CookieSettings.SaveCookie(countryCode);
System.Web.HttpContext.Current.Response.RedirectPermanent(redirectUrl);
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用 ASP.NET Core 中的中间件重写上面的代码?
我刚刚部分阅读了这篇文章:https : //docs.microsoft.com/en-us/aspnet/core/migration/http-modules
您几乎只需要将代码移动到中间件类,并使用 Core HttpContext 而不是 System.Web 。
这样的类看起来像这样:
//重定向中间件.cs
public class RedirectMiddleware
{
private readonly RequestDelegate _next;
public RedirectMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
string CountryCodeInUrl = "", redirectUrl = "";
var countryCode = CookieSettings.ReadCookie();
if (countryCode == "")
{
countryCode = "gb";
}
if (context.Request.Path.Value.Length >= 2)
{
CountryCodeInUrl = context.Request.Path.Value.Substring(1, 2);
}
if (countryCode != CountryCodeInUrl)
{
if (context.Request.Path.Value.Length >= 2)
{
if (context.Request.Path.Value.Substring(1, 2) != "")
{
countryCode = context.Request.Path.Value.Substring(1, 2);
}
}
if (!context.Request.Path.Value.Contains(countryCode))
{
redirectUrl = string.Format("/{0}{1}", countryCode, context.Request.Path.Value);
}
else
{
redirectUrl = context.Request.Path.Value;
}
CookieSettings.SaveCookie(countryCode);
context.Response.Redirect(redirectUrl, true);
}
await _next.Invoke(context);
}
}
Run Code Online (Sandbox Code Playgroud)
要使用它,请在注册 MVC 中间件之前在 Startup.cs 文件中注册它,如下所示:
app.UseMiddleware<RedirectMiddleware>();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
我希望这能让您入门,您可以查看此博客文章以获取有关中间件的更多信息。
只要您在触发的其他中间件之前插入重写中间件,您就可以重写Context.Request.Path中间件代码。
该路径是可写的,任何后续代码/中间件都将使用新路径来处理请求。
使用独立的(app.Use())中间件,您可以执行以下操作:
app.Use(async (context,next) =>
{
var url = context.Request.Path.Value;
// Rewrite privacy URL to index
if (url.Contains("/home/privacy"))
{
// rewrite to index
context.Request.Path = "/home/index";
}
await next();
});
Run Code Online (Sandbox Code Playgroud)
与Repsonse.Redirect()(触发新的服务器请求)不同,此操作不会更改原始请求的 URL。
这篇博文中的更多信息:
| 归档时间: |
|
| 查看次数: |
7628 次 |
| 最近记录: |