MVC区域的后备路线 [英] Fallback route for MVC Areas

查看:96
本文介绍了MVC区域的后备路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处使用asp.net核心MVC应用程序.我已经定义了我的区域,并且路由工作正常.

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.

我具有以下结构:

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.

用户当前浏览SomeArea1并单击Login. SomeArea1没有LoginController,他被定向到SharedArea\Login.

User currently browse SomeArea1 and clicks on Login. There is no LoginController for SomeArea1 and he gets directed to SharedArea\Login.

用户当前浏览SomeArea2并单击Login. SomeArea2有一个LoginController,他被引导到SomeArea2\Login.

User currently browse SomeArea2 and clicks on Login. There is aLoginController for SomeArea2 and he gets directed to SomeArea2\Login.

如何在Startup.cs文件中配置路由?

How would one go about configuring your routes in the Startup.cs file?

我在startup.cs中设置的当前路线:

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是子域路由的自定义实现,您可以在此处查看更多信息:

areaRouter is a custom implimentation for subdomain routing, you can view more here: subdomain routing to areas

推荐答案

我想做的一件事是有一个备用路线,以防 该区域不存在.

One thing i would like to do, is have a fallback route in case the area does not exist.

为每个区域安装基本控制器.这样,这些区域中的控制器将使用基本控制器中的HandleAttribute.只有一个设置(实际上每个区域一个)

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)

场景1 :在您所在的区域创建一个基本(共享)控制器:

Scenario1: Create a base(shared) Controller in your area:

[Authorize(ActiveAuthenticationSchemes = "sharedarea")]//see solution for next scenario
public class SharedAreaController : Controller
{
    [AllowAnonymous]
    public login()
    {

    }
}

从此控制器派生SomeArea1控制器:

Derive SomeArea1 controllers from this controller:

public class HomeController : SharedAreaController 
{
    // actions for this controller
}

场景2 :在您注册CookieAuthentication中间件的位置,执行以下操作:

Scenario2: The place where you register you CookieAuthentication middleware, do this:

app.UseCookieAuthentication(o =>
{
    o.LoginPath = "/SomeArea2/login";
    o.AuthenticationScheme = "SomeArea2";
    //TODO: set other interesting properties if you want to
});

然后在您的控制器/操作上,指定身份验证方案,例如:

then On you controller/action, specify the authentication scheme like:

[Authorize(ActiveAuthenticationSchemes = "SomeArea2")]
public IActionResult PageWhichNeedsArea2Auth()
{
    return Content("sth");
}

希望对您有所帮助:)

这篇关于MVC区域的后备路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆