Blazor 中的自定义 URL 重写规则 ASP.Net 核心(服务器端)在使用导航链接时未触发
Custom URL rewrite rule in Blazor ASP.Net Core (server-side) not triggering when using navlink
我在我的解决方案中设置了以下 IRule
:
public class IsUrlEncodedRule : Microsoft.AspNetCore.Rewrite.IRule
{
public void ApplyRule(RewriteContext context)
{
var request = context.HttpContext.Request;
var host = request.Host;
Console.WriteLine("Hello world!");
context.Result = RuleResult.ContinueRules;
}
}
然后在我的 Startup.cs
我有以下...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseHttpsRedirection();
app.UseRewriter(new RewriteOptions().Add(new IsUrlEncodedRule()));
app.UseStaticFiles();
...
app.UseMvc();
...
}
最终我希望规则检查是否对任何 URL 进行了编码,但现在我只想在控制台中看到 "Hello world" 以了解它是否正常工作,或者让它命中我在规则中的断点。这是一个服务器端 blazor 应用程序,现在唯一一次命中规则是当您最初导航到该站点时,或者如果您通过在导航栏中键入 url 手动导航到该站点的特定页面在浏览器中并手动导航。但是,如果您使用 NavLink
在站点内导航,则永远不会触发该规则,因为客户端永远不会发出任何其他请求,因为它都是通过 SignalR 处理的。
示例:
<NavLink class="nav-link" href="s3contents">
<span class="fas fa-bold" aria-hidden="true"></span> S3 Contents
</NavLink>
如果您已经访问该网站,单击此 NavLink
将带您到 http://localhost:8080/s3contents
,规则将 不被触发。到达那里后,如果您手动刷新页面,或者如果您复制 URL 并将其粘贴到新选项卡并导航到它,规则 将 被触发。我的问题是如何配置我的服务,以便我实施的任何 IRule
将 总是 被触发,即使用户已经导航到特定页面 在网站上,因此通过 SignalR 导航而不是传统的 http 请求?
Url rewrite 在收到 http 请求时在服务器中重写 url。
通过 SignalR 导航不会发出 http 请求,因此您不能使用 URL 重写您的行为。
但如果目标是捕获每个导航事件,请使用NavigationManager.LocationChanged
. It's not cancellable at the moment, and we cannot prevent navigation. But an issue is open https://github.com/aspnet/AspNetCore/issues/14962
我在我的解决方案中设置了以下 IRule
:
public class IsUrlEncodedRule : Microsoft.AspNetCore.Rewrite.IRule
{
public void ApplyRule(RewriteContext context)
{
var request = context.HttpContext.Request;
var host = request.Host;
Console.WriteLine("Hello world!");
context.Result = RuleResult.ContinueRules;
}
}
然后在我的 Startup.cs
我有以下...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseHttpsRedirection();
app.UseRewriter(new RewriteOptions().Add(new IsUrlEncodedRule()));
app.UseStaticFiles();
...
app.UseMvc();
...
}
最终我希望规则检查是否对任何 URL 进行了编码,但现在我只想在控制台中看到 "Hello world" 以了解它是否正常工作,或者让它命中我在规则中的断点。这是一个服务器端 blazor 应用程序,现在唯一一次命中规则是当您最初导航到该站点时,或者如果您通过在导航栏中键入 url 手动导航到该站点的特定页面在浏览器中并手动导航。但是,如果您使用 NavLink
在站点内导航,则永远不会触发该规则,因为客户端永远不会发出任何其他请求,因为它都是通过 SignalR 处理的。
示例:
<NavLink class="nav-link" href="s3contents">
<span class="fas fa-bold" aria-hidden="true"></span> S3 Contents
</NavLink>
如果您已经访问该网站,单击此 NavLink
将带您到 http://localhost:8080/s3contents
,规则将 不被触发。到达那里后,如果您手动刷新页面,或者如果您复制 URL 并将其粘贴到新选项卡并导航到它,规则 将 被触发。我的问题是如何配置我的服务,以便我实施的任何 IRule
将 总是 被触发,即使用户已经导航到特定页面 在网站上,因此通过 SignalR 导航而不是传统的 http 请求?
Url rewrite 在收到 http 请求时在服务器中重写 url。
通过 SignalR 导航不会发出 http 请求,因此您不能使用 URL 重写您的行为。
但如果目标是捕获每个导航事件,请使用NavigationManager.LocationChanged
. It's not cancellable at the moment, and we cannot prevent navigation. But an issue is open https://github.com/aspnet/AspNetCore/issues/14962