简体   繁体   English

没有带有默认路由的 AreaAttribute 的 ASP.Net 核心区域路由

[英]ASP.Net Core Area Routing without AreaAttribute with default routes

I have a problem regarding AreaRouting in my ASP.Net Core MVC project.我在我的 ASP.Net Core MVC 项目中遇到了有关 AreaRouting 的问题。 I want to use Area routing without adding AreaAttribute for each controller (I move from an existing ASP.NET MVC 5 project with hundreds of controller and adding this attribute is really cumbersome)我想在不为每个控制器添加 AreaAttribute 的情况下使用区域路由(我从一个现有的具有数百个控制器的 ASP.NET MVC 5 项目移动,添加这个属性真的很麻烦)

My sample application folder structure looks like this:我的示例应用程序文件夹结构如下所示: 在此处输入图片说明

The namespaces follow the folder structure.命名空间遵循文件夹结构。 I found a way to add a custom convention and extract the area name:我找到了一种添加自定义约定并提取区域名称的方法:

internal sealed class AreaRoutingConvention : IControllerModelConvention
    {
        public void Apply(ControllerModel controller)
        {
            var hasRouteAttributes = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);

            if (hasRouteAttributes)
            {
                return;
            }

            var controllerTypeNamespace = controller.ControllerType.Namespace;
            if (controllerTypeNamespace == null)
                return;

            var areaName = controllerTypeNamespace.Split('.').SkipWhile(segment => segment != "Areas").Skip(1).FirstOrDefault();

            
            if (!string.IsNullOrEmpty(areaName))
            {
                var template = areaName + "/[controller]/[action]/{id?}";
                controller.RouteValues.Add("area", areaName);

                foreach (var selector in controller.Selectors)
                {
                    selector.AttributeRouteModel = new AttributeRouteModel
                    {
                        Template = template
                    };
                }
            }
        }
    }

My sample route configuration is this one:我的示例路由配置是这样的:

app.UseEndpoints(endpoints =>
            {
                endpoints.MapAreaControllerRoute(
                    name: "Activity_default",
                    areaName: "Activity",
                    "Activity/{controller=Activity}/{action=Index}/{id?}",
                    defaults: new { controller = "Activity", action = "Index" }
                );
                
                endpoints.MapAreaControllerRoute(
                    name: "Admin_default",
                    areaName: "Admin",
                    "Admin/{controller=Admin}/{action=Index}/{id?}",
                    defaults: new { controller = "Admin", action = "Index" }
                );

                endpoints.MapAreaControllerRoute(
                    name: "home_default",
                    areaName: "Home",
                    pattern: "{area}/{controller=Home}/{action=Index}/{id?}");
            });

It works in almost all of my cases: Now when I enter它几乎适用于我的所有情况:现在当我进入

  • /Activity/Activity/Index -> Index method is invoked /Activity/Activity/Index -> Index 方法被调用
  • /Activity/Home/Index -> Not found, as intended /Activity/Home/Index -> 未找到,如预期
  • /Home/Home/Index -> Index method is invoked /Home/Home/Index -> Index 方法被调用
  • /Home/Activity/Index -> Not found, as intended /Home/Activity/Index -> 未找到,如预期
  • /Admin/Admin/Index -> Index method is invoked /Admin/Admin/Index -> Index 方法被调用
  • /Admin/Activity/Index -> Not found, as intended /Admin/Activity/Index -> 未找到,如预期

But I also want to route to the /Activity/Activity/Index method when the user enters但我也想在用户输入时路由到 /Activity/Activity/Index 方法

  • /Activity /活动
  • /Activity/Activit/ /活动/活动/

Can anyone please help with this problem?任何人都可以帮忙解决这个问题吗? When I do not use my custom convention and add Area-Attributes to the controllers all works well.当我不使用我的自定义约定并将区域属性添加到控制器时,一切正常。

You can try route attributes您可以尝试路由属性

public partial class ActivityController : Controller
    {

        [Route("~/Activity")]
        [Route("~/Activity/Activity")]
        [Route("~/Activity/Activity/Index")]
        public ActionResult Index()
        {
            ....
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM