Reputation: 134
Routing using MapControllerRoute no longer works after migrating from .NET Core 3.1 app to .NET 6. The app is still using Startup.cs, and contains MVC, Razor Pages (for Identity), and Blazor server. Attribute routing still continues to work, as does Blazor routing (but that uses the @page routing on ever razor file).
Inside public void Configure I have: app.UseRouting(); and
// upgrading from 2.2 to 3.1 we need to use UseEndpoints since UseMvc was deprecated.
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("areas", "{area:exists}/{controller=MainMenu}/{action=Index}/{id?}");
endpoints.MapControllerRoute("default", "{controller=Research}/{action=Index}/{id?}");
endpoints.MapRazorPages(); // after upgrading from 2.2 to 3.1, we need to use MapRazorPages() to map the authentication razor pages.
endpoints.MapBlazorHub();
// Creates a low-priority route to the _Host file. In effect, all traffic that isn't explicitly routed
// elsewhere will go to _Host, where Blazor's internal routing will take over. This means your existing
// MVC routes will continue to work, *except* the default Route, which is a problem and we will work
// around that in the next commit.
endpoints.MapFallbackToPage("/_Host");
}
The comments above endpoints.MapFallbackToPage("/_Host"); were from documentation on how to add Blazor to an existing project from when I first added Blazor to the solution more than a year ago. The default route routing has been working. I thought perhaps this was interfering with .NET 6, but I commented out the Blazor endpoint routes as well as services.AddServerSideBlazor and my routing continued not to work. I am not sure what else to try aside from adding Attribute routing to all of my controller Index methods throughout the entire (large) application.
Visual Studio Version 17.4.2
All relevant Nuget packages have been updated to version 6.0.11 (the most recent for .NET 6).
This a link to relevant controller routing in .NET 6, and as far as I can tell my setup seems to be okay.
I am not sure what else to troubleshoot. Any insights or help will be greatly appreciated.
Ex 1. Navigate to /IPODB (area name)
Expected Behavior: Display the Index.cshtml page for the MainMenu controller inside of the IPODB area
Actual Behavior: Blank page, Network tab in Web Developer Tools shows "No requests." Page loads if URL is /IPODB/MainMenu or /IPODB/MainMenu/Index. If attribute [Route("[area]")] is added to the controller Index method, routing properly works. But only for that route, no longer for /IPODB/MainMenu or /IPODB/MainMenu/Index like in ASP.NET Core 3.1. In addition to all 3 URLs working in 3.1, the URL built from using Link Name would be /IPODB, whereas if I were to add multiple route attributes, it always defaults to the one with most levels instead of least (so /IPODB/MainMenu/Index).
Ex 2. Navigate to /Research
Expected Behavior: Display the Index.cshtml page for the Research controller in the projects root Controllers folder.
Actual Behavior: Blank page, Network tab in Web Developer Tools shows "No requests." If user navigates to /Research/Index the page loads properly. If attribute [Route("[controller]")] is added to the controller Index method, routing properly works, unless /Index is appended to the end. Same reasoning as above.
Upvotes: 2
Views: 2191
Reputation: 11631
In .Net 6, You could try set as below:
.......
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapAreaControllerRoute(
name: "areas",
areaName: "IPODB",
pattern: "{area}/{controller=MainMenu}/{action=Index}/{id?}"
);
app.Run();
Result:
In Startup class, you could also try as below:
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute("areas", "IPODB","{area}/{controller=MainMenu}/{action=Index}/{id?}");
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
It performed the same
Upvotes: 0