Get current Url in a Blazor component

前端 未结 3 1267
渐次进展
渐次进展 2020-11-30 03:35

I need to know the url of the current page in order to check if I have to apply certain style to a element. The code below is an example.

@using Microsoft.As         


        
相关标签:
3条回答
  • 2020-11-30 04:17

    There is no use in connecting to the OnLocationChanged event in a page or component, as they get loaded and disposed on demand.

    You should register to this event in app.cshtml as that won't be disposed.

    0 讨论(0)
  • 2020-11-30 04:30

    You should listen to a LocationChange of the IUriHelper, which triggers the function to do what you want for example:

    @using Microsoft.AspNetCore.Blazor.Components
    @using Microsoft.Extensions.Logging
    @inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper
    @inject ILogger<NavItem> logger
    
    <li class="m-menu__item @(Active ? "m-menu__item--active" : "")">
        <a href=@Url class="m-menu__link ">
            <span class="m-menu__item-here"></span>
            <i class="m-menu__link-icon @Icon"></i>
            <span class="m-menu__link-text">@Text</span>
        </a>
    </li>
    
    @functions {
        protected override void OnInit()
        {
            UriHelper.OnLocationChanged += OnLocationChanges;
        }
        [Parameter]
        private string Url { get; set; }
        [Parameter]
        private string Icon { get; set; }
        [Parameter]
        private string Text { get; set; }
        private bool Active = false;
        private void OnLocationChanges(object sender, string newLocation)
        {
            bool active = newLocation.Contains(Url);
            if(active != Active) //Only re-render the components that need it
            {
                Active = active;
                StateHasChanged();
                logger.LogInformation("Location Change To:" + newLocation);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 04:37

    Use Uri property from NavigationManager class.

    How it works

    Get it from injection before to use it on .razor pages:

    @inject NavigationManager MyNavigationManager
    

    Or like this on .cs if you prefer "code-behind" experience:

    using Microsoft.AspNetCore.Components;
    ...
    [Inject] 
    public NavigationManager MyNavigationManager {get; set;}
    

    Sample

    @page "/navigate"
    @inject NavigationManager MyNavigationManager
    
    <h1>Current url</h1>
    
    <p>@(MyNavigationManager.Uri)</p>
    
    

    More about navigation (NavigateTo, BaseUri, ToAbsoluteUri, ToBaseRelativePath, ... ) at: URI and navigation state helpers

    NavigationManager cheatsheet

    MyNavigationManager.Uri
    #> https://localhost:5001/counter/3?q=hi
    
    MyNavigationManager.BaseUri`
    #> https://localhost:5001/
    
    MyNavigationManager.NavigateTo("http://new location")
    #> Navigates to new location
    
    MyNavigationManager.LocationChanged
    #> An event that fires when the navigation location has changed.
    
    MyNavigationManager.ToAbsoluteUri("pepe")
    #> https://localhost:5001/pepe
    
    MyNavigationManager.ToBaseRelativePath(MyNavigationManager.Uri)
    #> counter/3?q=hi
    
    Helper: AddQueryParm( "q2", "bye" )
    #> https://localhost:5001/counter/3?q=hi&q2=bye
    
    Helper: GetQueryParm( "q" )
    #> hi
    

    Helpers code:

    @code {
    
    [Parameter]
    public string Id { get; set; }
    
    // blazor: add parm to url
    string AddQueryParm(string parmName, string parmValue)
    {
        var uriBuilder = new UriBuilder(MyNavigationManager.Uri);
        var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
        q[parmName] = parmValue;
        uriBuilder.Query = q.ToString();
        var newUrl = uriBuilder.ToString();
        return newUrl;
    }
    
    // blazor: get query parm from url
    string GetQueryParm(string parmName)
    {
        var uriBuilder = new UriBuilder(MyNavigationManager.Uri);
        var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
        return q[parmName] ?? "";
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题