Hi Guys,
I have a problem with routing in my application. Basically I set up my endpoint routing in Startup.cs but it always gets ignored. The patterns simply do not change anything and I am forced to use attribute routing.
If I do not use the attribute routing I get absolutely random results for top level controllers (those not in the Areas), as if patters were being matched against random controllers. For example I press Edit button in one of the views from a Controller1 (the URL is correct, has correct action and all) and page from a Controller2 opens or I get null exception error (because a random page from another controller got matched - another top level controller).
Here are the patterns I am using:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "areaRoute",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
endpoints.MapHub<ChatHub>("/chatHub");
endpoints.MapHub<DeviceStatusHub>("/deviceStatusHub");
});
I have also tried:
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
endpoints.MapControllerRoute(
name: "areaRoute",
pattern: "{area:exists}/{controller}/{action}/{id?}",
defaults: new { action = "Index" });
But unless I put attribute routing in my controllers (both 'top level' and ones in areas) I get the aforementioned randomness or error 404 if it comes to areas (usually then the area name is passed in the URL as a query string instead of being inserted before the controller as in ../AreaControllerName?Area=AreaName instead of ../AreaName/AreaControllerName)
So before each controller I now have to include routing like so:
[Area("MyArea")]
[Route("MyAreaController/{action}")]
[Route("MyAreaController/{action}/{id?}")]
public class MyAreaController: Controller (...)
I even had to start doing that for the top level controllers (as in those not in the areas) otherwise I get the random results mentioned above.
I am going a bit mad here, never faced that problem - this is not a first app I am writing in ASP.NET Core (been working with it since v 1.1) I am absolutely stumped as to what is going on.
Any help or guidance would be greatly appreciated.