-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Make link creation typesafe using expressions #14665
Copy link
Copy link
Open
Labels
affected-mediumThis issue impacts approximately half of our customersThis issue impacts approximately half of our customersarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-routingseverity-majorThis label is used by an internal toolThis label is used by an internal tool
Milestone
Metadata
Metadata
Assignees
Labels
affected-mediumThis issue impacts approximately half of our customersThis issue impacts approximately half of our customersarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-routingseverity-majorThis label is used by an internal toolThis label is used by an internal tool
Type
Fields
Give feedbackNo fields configured for Feature.
Currently there is no refactor-safe way to create links to actions or pages, it has to be done using magic strings that the compiler doesn't understand. For example, I have an
AboutControllerwith anIndexaction, then I can create a link usingUrl.Action("Index", "About"). But if I rename the action or controller then this will lead to a runtime exception, not a compile time exception. I could useUrl.Action(nameof(AboutController.Index), "About"), but usingnameof()only works for the action, not the controller, since it expects me to specify the name of the controller without the Controller suffix. This is further a problem for actions that take query parameters, where they are specified using an anonymous class with no type safety at all. That is, there is no verification thatUrl.Action("Index", "Search", new {name, title, query})will create a link that sends the correct query parameters to theSearchController.Indexmethod.My proposal is therefore a method that takes an expression, so that it can look like this:
The
UrlHelper.Actionmethod would be defined aspublic static string Action<TC>(this IUrlHelper helper, Expression<Func<TC, object>> expression)or something similar, and based on the expression passed into it enough information should be able to be gathered to create a url.