本文介绍了使用navlink时不会触发Blazor ASP.Net Core(服务器端)中的自定义URL重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解决方案中设置了以下 IRule :

I have the following IRule set up in my solution:

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 中,我有以下内容...

And then in my Startup.cs I have the following...

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处理的.

Eventually I want the rule to check that any URLs are encoded but for now I just want to see the "Hello world" in the console to know that its working, or for it to hit my breakpoints in the rule at all. This is a server-side blazor app and right now the only time the rule is hit is when you initially navigate to the site or if you manually navigate to a particular page of the site by typing the url in the nav bar in the browser and navigating manually. If, however, you use a NavLink to navigate within the site, the rule is never triggered since the client is never making any additional requests since its all handled through 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请求进行导航?

If you are already on the website, clicking this NavLink will take you to http://localhost:8080/s3contents and the rule will not be triggered. Once you are there, if you refresh the page manually or if you copy the URL and paste it into a new tab and navigate to it, the rule will be triggered. My question is how do I configure my services such that any IRule I implement will always be triggered even if a particular page was navigated to by a user already on the website and therefor navigating via SignalR instead of a traditional http request?

推荐答案

当收到http请求时,URL重写会重写服务器中的url.
通过SignalR导航不会发出http请求,因此您不能使用URL改写来实现您的行为.

Url rewrite rewite the url in the server when a http request is received.
Navigation through SignalR do not make http request so you cannot use URL rewritting for your behavior.

但是,如果目标是捕获每个导航事件,请使用 NavigationManager.LocationChanged .目前无法取消,我们也无法阻止导航.但是存在一个问题 https://github.com/aspnet/AspNetCore/issues/14962

But if the goal is to capture each navigation event, Use 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

这篇关于使用navlink时不会触发Blazor ASP.Net Core(服务器端)中的自定义URL重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:04