Using asp.net core MVC application here. I have my areas defined and routing works as expected.
One thing i would like to do, is have a fallback route in case the area does not exist.
I have the following structure:
APP
-- Areas
-- SharedArea
-- Controllers
-- LoginController
-- UserController
-- AnotherController
-- AndSoOnController
-- SomeArea1
-- Controllers
-- HomeController
-- SomeArea2
-- Controllers
-- HomeController
-- LoginController
My ultimate goal, is to have shared controllers that is being used as a fallback in the event that an area does not have the specified controller.
Scenario 1
User currently browse SomeArea1
and clicks on Login
. There is no LoginController
for SomeArea1
and he gets directed to SharedArea\Login
.
Scenario 2
User currently browse SomeArea2
and clicks on Login
. There is aLoginController
for SomeArea2
and he gets directed to SomeArea2\Login
.
How would one go about configuring your routes in the Startup.cs
file?
My current route setup in startup.cs
:
app.UseMvc(routes =>
{
routes.DefaultHandler = areaRouter;
routes.MapRoute("areaRoute", "{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
areaRouter
is a custom implimentation for subdomain routing, you can view more here: subdomain routing to areas
Implement the base controllers for each area. In this way the controllers from those areas will use the
HandleAttribute
from the base controller. It's only 1 setting (one for each area in fact)Scenario1: Create a base(shared) Controller in your area:
Derive SomeArea1 controllers from this controller:
Scenario2: The place where you register you
CookieAuthentication
middleware, do this:then On you controller/action, specify the authentication scheme like:
I hope to be helpful :)