-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Feature request: strongly-typed route values in a
tag-helpers
#35376
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
Comments
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Tag helpers are great but refactoring them is always a pain. Maybe source generators could be used here? |
Interestingly, this post from the .NET Blog: Introducing C# Source Generators suggests exactly that:
|
(I'll get off topic here a bit.) Yup, this is true. As opposed to Razor syntax, I prefer systems like JSX which look like markup but actually expand into regular JavaScript code. I wish we had something like this in ASP.NET Core. Something like JSX (csx?) which simply expands into C#. This way the page description code is fully in C# and thus you have the full power of the language to perform whatever refactoring you'd like. For this reason, I like: https://github.com/giraffe-fsharp/Giraffe You write your page code in straight F#. So very powerful refactoring. However, it's not as mature as ASP.NET Core. E.g. nothing like tag-helpers there. |
As an aside, I've been exploring an approach to getting views that are fully type-safe by having everything expressed in C# instead of Razor syntax. Video I recorded demonstrating this approach: |
I think this approach goes a little bit too far for me. I am into type-safe tag helpers not complete overhaul of whole Razor. Maybe first step would be some extension of IHtmlHelper? Tag helpers use internally IHtmlHelper. |
Oh, I completely understand that such a radical approach may not be feasible for some folks. :-) Was just showing an approach I've been exploring to work around the lack of type-safety. |
Thanks for contacting us. We're moving this issue to the |
Hi. Thanks for contacting us. |
The Issue
The example project from the Razor Pages with Entity Framework Core in ASP.NET Core - Tutorial includes the following code:
Link to those lines in the project.
The route parameter names:
are not strongly-typed. I.e. there could be a typo in any of the names (
sortOrder
,pageIndex
,currentFilter
) and the project will compile and run however the program will not function as expected.Workaround - use
LinkGenerator.GetPathByAction
This stackoverflow post discusses one workaround for the issue. The example there has the following
a
tag-helper:It is rewritten in a more strongly-typed manner using
LinkGenerator.GetPathByAction
as follows:This is more of a demonstration of what is theoretically possible in the form of a quick solution; it's not ideal for end users.
Workaround - R4MVC
Another approach is to use R4MVC.
Here's a video demonstrating R4MVC.
Downsides to R4MVC:
Workaround - object for parameters
If we use a class to represent the parameters to
OnGetAsync
:then instead of:
we can use
asp-all-route-data
and generate a dictionary from an instance ofOnGetAsyncParameters
:Link
where
to_dict
is the following utility function:In this case,
OnGetAsync
is updated to acceptOnGetAsyncParameters
:Link
Request
The request is for ASP.NET Core to have route parameters be more strongly-typed in
a
tag-helpers.I understand that the priority may be low. I'd just like to request that it at least be on the roadmap. :-)
The text was updated successfully, but these errors were encountered: