在Blazor组件中获取当前Url

时间:2018-04-30 14:35:32

标签: c# asp.net-core blazor

我需要知道当前页面的url,以便检查是否必须将某种样式应用于元素。下面的代码就是一个例子。

@using Microsoft.AspNetCore.Blazor.Services
@inject IUriHelper UriHelper
@implements IDisposable

<h1>@url</h1>
<nav>
    <div class="nav-wrapper">
        <a href="#" class="brand-logo">Blazor</a>
        <ul id="nav-mobile" class="right hide-on-med-and-down">
            <li>
                <NavLink href="/" Match=NavLinkMatch.All>
                    Home
                </NavLink>
            </li>
            <li>
                <NavLink href="/counter">
                    Counter
                </NavLink>
            </li>
            <li>
                <NavLink href="/fetchdata">
                    Fetch data
                </NavLink>
            </li>
        </ul>
    </div>
</nav>

@functions {

    private string url = string.Empty;

    protected override void OnInit()
    {
        url = UriHelper.GetAbsoluteUri();
        UriHelper.OnLocationChanged += OnLocationChanged;
    }

    private void OnLocationChanged(object sender, string newUriAbsolute)
    {
        url = newUriAbsolute;
    }

    public void Dispose()
    {
        UriHelper.OnLocationChanged -= OnLocationChanged;
    }
}

我使用了Blazor存储库中NavLink组件中使用的相同方法,但它没有用。有什么想法?。

3 个答案:

答案 0 :(得分:7)

简而言之,请使用IUriHelper中的GetAbsoluteUri():

1)不要忘记在.cshtml的注入中设置UriHelper:

@inject Microsoft.AspNetCore.Components.Services.IUriHelper UriHelper

//for <= 0.7.0 versions use:
//@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper

或在基类继承中获得“代码隐藏”体验.cs

[Inject] 
public Microsoft.AspNetCore.Components.Services.IUriHelper UriHelper {get; set;}

//for <= 0.7.0 versions use:
//[Inject] 
//public Microsoft.AspNetCore.Blazor.Services.IUriHelper UriHelper {get; set;}

2)调用GetAbsoluteUri方法:

string currentUrl = UriHelper.GetAbsoluteUri()

已编辑 On 0.8.0, IUriHelper has moved to Microsoft.AspNetCore.Components.Services namespace

答案 1 :(得分:1)

连接页面或组件中的OnLocationChanged事件没有任何用处,因为它们会在demannd中加载和处理。

您应该在app.cshtml中注册此活动,因为这不会被处理。

答案 2 :(得分:0)

你应该听一下IUriHelper的LocationChange,它会触发你想要的功能,例如:

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