ASP.NET Core MVC : UrlHelper.Action fail to resolve when mixing nullable route value and query string parameters
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
I have a ASP.NET Core MVC .NET5 application with a simple route. It is the default controller/action pattern with an optional integer parameter. It is configured as:
`endpoints.MapControllerRoute("default", "{controller=Account}/{action=Login}/{enterpriseId:int?}");`
And a test Controller with two actions, one without query string parameters, one with one:
```
public class TestController : Controller
{
public ActionResult Test()
{
return View();
}
public ActionResult TestQuery(int test)
{
return View();
}
}
```
I can access the site via those URL successfully:
> https://localhost/Test/Test
> https://localhost/Test/Test/1
> https://localhost/Test/TestQuery?test=2
> https://localhost/Test/TestQuery/1?test=2
The versions without the /1 doesn't contains a RouteData entry for enterpriseId value, the others do, which is fine.
When using UrlHelper.Action(), the parameter "values" allow to pass both Route data entries and query strings. But somehow it fails to resolve when setting the RouteData entry to null only in the specific case that it also contains query parameters.
```
urlHelper.Action("Test", "Test", new RouteValueDictionary { { "enterpriseId", 1 } }); // Returns "/Test/Test/1"
urlHelper.Action("Test", "Test", new RouteValueDictionary { }); // Returns "/Test/Test"
urlHelper.Action("Test", "Test", new RouteValueDictionary { { "enterpriseId", null } }); // Returns "/Test/Test"
urlHelper.Action("TestQuery", "Test", new RouteValueDictionary { { "enterpriseId", 1 }, { "test", 2 } }); // Returns "/Test/TestQuery/1?test=2"
urlHelper.Action("TestQuery", "Test", new RouteValueDictionary { { "test", 2 } }); // Returns "/Test/TestQuery?test=2"
urlHelper.Action("TestQuery", "Test", new RouteValueDictionary { { "enterpriseId", null }, { "test", 2 } }); // Returns null
```
I would expect the last line to also returns "/Test/TestQuery?test=2" instead of null.
Why does the sixth line behave differently than the third one (both with enterpriseId set to null)? But it works when it is omitted? Why can't it resolve to the URL I expect?
Is this a bug in the framework or something I misunderstand?
Worth mentioning that in the previous ASP.NET version (non-Core), using UrlHelper.GenerateUrl() was working fine with all cases.
Contributor guide
Assessment
This issue has not been assessed yet.