Asked by:
MVC core 2.2 conventional routing issue

Question
-
User-1220080607 posted
conventional routes:
app.UseMvc(routes => { routes.MapRoute(name: "person", template: "/person/{info}", defaults: new { controller = "Person", action = "Index" }); routes.MapRoute(name: "custom_page", template: "/{key1}/{key2}", defaults: new { controller = "Custom", action = "Index" }); });
While using dotnet core 2.1
1. 'example.com/person/name' goes to controller Person
2. 'example.com/customPage/index' goes to controller Custom
here it working ,conventional routing based on order of declared routes take first priority
While using dotnet core 2.2
1. 'example.com/person/name' goes to controller Custom (Expected to go to Person Controller)
2. 'example.com/customPage/index' goes to controller Custom
why in 2.2 Conventional based routing order not working?
Thursday, August 1, 2019 10:39 AM
All replies
-
User475983607 posted
I cannot reproduce this issue given the route table shared in your original post. Entering example.com/person/name in the browser address bar invokes the person controller in ASP.NET Core 2.2. Double check your test.
ASP.NET 2.2 configuration
app.UseMvc(routes => { routes.MapRoute( name: "person", template: "/person/{info}", defaults: new { controller = "Person", action = "Index" }); routes.MapRoute( name: "custom_page", template: "/{key1}/{key2}", defaults: new { controller = "Custom", action = "Index" }); });
Controllers
public class PersonController : Controller { public IActionResult Index(string info) { return Json(new { controller = "Person", parameter = info }); } }
public class CustomController : Controller { public IActionResult Index(string key1, string key2) { return Json(new { controller = "Custom", key1 = key1, key2 = key2}); } }
Thursday, August 1, 2019 11:42 AM -
User711641945 posted
Hi Naveen,
I also test the code and do not reproduce the issue.I suggest that you need to check your code(eg. Action).Or could you share a simple demo that could reproduce your issue.
Here is my working demo:
1.Startup.cs:
app.UseMvc(routes => { routes.MapRoute(name: "person", template: "/home/{info}", defaults: new { controller = "Home", action = "Index" }); routes.MapRoute(name: "custom_page", template: "/{key1}/{key2}", defaults: new { controller = "SearchPage", action = "Index" }); });
2.HomeController:
public class HomeController : Controller
{
public IActionResult Index() { return View(); } public IActionResult Privacy() { return View(); }
}3.SearchPageController:
public class SearchPageController : Controller { public async Task<IActionResult> Index() { return View(); } }
When i use url like:example.com/home/privacy,it would get into the Home/Index.
When i use url like:example.com/page/index,it would get into the SearchPage/Index.
It works well by using your route template in .net core 2.2.
Best Regards,
Rena
Friday, August 2, 2019 2:05 AM -
-
User711641945 posted
Hi Naveen,
I suggest that you need follow the official document to migrate .net core 2.1 to .net core 2.2. Because I work fine in my working project.
Study the tutorial carefully and recheck if anything forgetting to configure.Here is a basic step to migrate:
1.Update Target Framework Moniker:
<TargetFramework>netcoreapp2.2</TargetFramework>
2.Update package references(all of the package version should be updated):
<ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" /> </ItemGroup>
3.Update compatibility version:
services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
More details you could refer to: https://docs.microsoft.com/en-us/aspnet/core/migration/21-to-22?view=aspnetcore-2.2&tabs=visual-studio
Best Regards,
Rena
Friday, August 2, 2019 8:34 AM -
User-1220080607 posted
Hi Rena Ni,
I followed the same documentation provided by Microsoft to upgrade 2.2
temporary fix for my code is below
services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
this is like fallback to 2.1 routings.
Friday, August 2, 2019 9:35 AM -
User711641945 posted
Hi Naveen,
services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
this is like fallback to 2.1 routings.
Yes.This option means you could use the routing logic of ASP.NET Core 2.1 or earlier.
Refer to: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-2.2
Best Regards,
Rena
Monday, August 5, 2019 8:03 AM