0

I have a ASP.NET Core MVC 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)? 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.

Dunge
  • 532
  • 3
  • 19
  • 1
    You might want to open an issue for this [on GItHub](https://github.com/dotnet/aspnetcore/issues) since this appears to be a bug. – poke Sep 09 '21 at 21:33
  • Thanks. Wanted to make sure I wasn't overlooking something first. I opened it: https://github.com/dotnet/aspnetcore/issues/36352 – Dunge Sep 09 '21 at 22:41

0 Answers0