简体   繁体   中英

Custom URL rewrite rule in Blazor ASP.Net Core (server-side) not triggering when using navlink

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

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

    ...

}

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.

Example:

<NavLink class="nav-link" href="s3contents">
    <span class="fas fa-bold" aria-hidden="true"></span> S3 Contents
</NavLink>

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM