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

Shareable string constant as route template #15665

Closed
MarekPokornyOva opened this issue Aug 25, 2018 · 6 comments
Closed

Shareable string constant as route template #15665

MarekPokornyOva opened this issue Aug 25, 2018 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@MarekPokornyOva
Copy link

Blazor project (ASP.NET Core hosted) (client project) v 0.5.1.

I wanted to create shareable route templates.

static class UiPaths
{
    public static string Account = "account";
}

Solution 1 - reference string constant in page:

  • Put @page UiPaths.Account to Account.cshtml.

Issue 1

  • Compiler complains error: The 'page' directive expects a string surrounded by double quotes.

Solution 2 - create code behind class:

	[Microsoft.AspNetCore.Blazor.Components.RouteAttribute(UiPaths.Account)]
	public abstract class AccountPageCodeBehind : Microsoft.AspNetCore.Blazor.Components.BlazorComponent
	{
	}

Remove all @page directives from Account.cshtml.
Put "@inherits AccountPageCodeBehind" to Account.cshtml.

Issue 2
The application throws exception in runtime:
Error: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.InvalidOperationException: The following routes are ambiguous:
'account' in 'BlazorInNetFW.Client.Pages.Account'
'account' in 'BlazorInNetFW.Client.Pages.AccountPageCodeBehind'

The problem is ComponentResolver is static class instead of replaceable service and it finds code behind class as a regular component. Then RouteTable creates RouteEntry even for the code behind class.


If I understand correctly, only possible solution here is to create and use custom router (instead of Microsoft.AspNetCore.Blazor.Routing.Router). However, I think it would be better if it's possible to maintain route paths/templates in configurable way (possibly even paths taken from a DB) instead of custom Router. Could you provide your opinion and outline code changes (if any), please?

@SteveSandersonMS
Copy link
Member

Thanks for suggesting this. I've added this requirement to our tracking issue at #5489.

In the meantime I'd recommend just duplicating the strings, as there isn't a straightforward way to share them.

@McHeff
Copy link

McHeff commented Apr 4, 2019

So I'm experiencing something super duper weird over here...

Things have been working just fine until I made a new page...

Now I'm getting this:

Error: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.InvalidOperationException: The following routes are ambiguous:
'editfibrebaletemplates/{Id}/{BaleCount}/{Lot}/{CreatedBy}/{Revision}/{SpinProcess}/{IsCurrent}/{IsApproved}' in 'PrillaWeb.Pages.Spinner_Operations.Engineered_Fibre_Selections.EditFibreBaleTemplates'
'editfibrebaletemplates/{Id}/{BaleCount}/{Lot}/{CreatedBy}/{Revision}/{SpinProcess}/{IsCurrent}/{IsApproved}' in 'PrillaWeb.Pages.Spinner_Operations.Engineered_Fibre_Selections.EditFibreBaleTemplates'

And this is the page it's complaining about:

image

As you can see, there is only one page declaration.
And as for my LoadPlanning page, which is the new one I made:

image

She also only has one page declaration?

WTF

@McHeff
Copy link

McHeff commented Apr 4, 2019

So... I figured out the issue. It has NOTHING to do with page declarations, it actually had to do with my for loop that I had:

foreach (var item in SalesOrderLine.Where(x => (x.Selected == true) && (x.SalesOrder == SearchValue))) { SalesOrderPallet = SalesOrderPallet.Where(x => x.StockCode == item.StockCode).ToList<SalesOrderPallets>(); }
image

Was causing the app to throw an ambiguous route error,

I then changed it to this:

foreach (var item in SalesOrderLine.Where(x => x.Selected == true)) { if(item.SalesOrder == SearchValue) { SalesOrderPallet = SalesOrderPallet.Where(x => x.StockCode == item.StockCode).ToList<SalesOrderPallets>(); } }

image

And it works fine now... Super weird?

@danroth27
Copy link
Member

Hi @McHeff. If you think you've discovered a bug, please file an issue on the https://github.com/aspnet/aspnetcore repo. Now that the Blazor code is part of the main ASP.NET Core repo, we no longer use this repo for Blazor issues.

@McHeff
Copy link

McHeff commented Apr 5, 2019

Sure thing @danroth27, it's really weird because this only happens sometimes... Like I have many razor views in my app, most of them have lambda expressions and nested for loops, but only on this new page did I get ambiguous page route errors...

On another page, I got that error when I put some binding on my HTML control. Lol, will file an issue now (y)

@StaticBR
Copy link

In Case anyone also stumble across this issue.
Add this to your Razor file, and you can use route constants:
@attribute [Microsoft.AspNetCore.Components.RouteAttribute(@UiPaths.Account)]

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 27, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Oct 27, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

6 participants