Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetRouteUrlHelper #252

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/BlazorWebFormsComponents/BaseWebFormsComponent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -44,6 +46,12 @@ void ParentWrappingBuildRenderTree(RenderTreeBuilder builder)

#region Obsolete Attributes / Properties

[Inject]
public LinkGenerator LinkGenerator { get; set; }

[Inject]
public IHttpContextAccessor HttpContextAccessor { get; set; }

/// <summary>
/// 🚨🚨 Use @ref instead of ID 🚨🚨
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Routing" Version="2.2.0" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup>

Expand Down
19 changes: 19 additions & 0 deletions src/BlazorWebFormsComponents/Extensions/GetRouteUrlHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Routing;

namespace BlazorWebFormsComponents.Extensions
{
public static class GetRouteUrlHelper
{
public static string GetRouteUrl(this BaseWebFormsComponent component, object routeParameters)
=> component.LinkGenerator.GetPathByRouteValues(component.HttpContextAccessor.HttpContext, null, routeParameters);

public static string GetRouteUrl(this BaseWebFormsComponent component, string routeName, object routeParameters)
=> component.LinkGenerator.GetPathByRouteValues(component.HttpContextAccessor.HttpContext, routeName, routeParameters);

public static string GetRouteUrl(this BaseWebFormsComponent component, RouteValueDictionary routeParameters)
=> null;

public static string GetRouteUrl(this BaseWebFormsComponent component, string routeName, RouteValueDictionary routeParameters)
=> null;
}
}