代码之家  ›  专栏  ›  技术社区  ›  Andez

在单个根文件夹下组织WebAPI控制器并路由到它们

  •  0
  • Andez  · 技术社区  · 7 年前

    背景

    最多 MVC WebApi 应用程序我们通常看到以下结构:

    /Controllers
      HomeController.cs
    /Models
    

    通常,这是从MVC模板生成的。

    这将在中生成路由映射 Startup.cs :

    app.UseMvc(routes =>
    {
        routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
    });
    

    增加了复杂性

    当我们向其添加其他部分(如自定义操作结果)时,我们的Web应用程序确实开始变得越来越复杂, Filters 以及其他领域。突然之间,顶级文件夹会感觉有点杂乱,即使它组织得很好。

    问题

    通常当我们添加 Area 开箱即用,创建一个名为“区域”的新顶级文件夹。

    我的首选方案是将所有与控制器相关的功能都移到一个项目文件夹中,比如一个API文件夹。

    例如:

    /api
      /Home
        /Controllers
          HomeController.cs
        /Models
      /SomeArea1
        /Controllers
        /Models
    

    现在的问题是,您需要更改路由配置并在路由中包含API-我 想要。

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "areaRoute",
            template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
    

    是否可以在项目中具有上述文件夹结构,并具有如下路径?

    http://localhost/ maps to /api/Home 
    http://localhost/customer maps to /api/Customer
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Mads...    7 年前

    您可以在下面的代码中路由类似的操作

    app.UseMvc(routes =>
    {
       routes.MapRoute("blog", "blog/{*article}",
                   defaults: new { controller = "Blog", action = "Article" });
       routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
    });
    

    请检查这里的文件

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1#route-names

        2
  •  0
  •   Andez    7 年前

    从MADS提供的关于控制器路由的部分( see here - customise routes )

    名称空间路由约定.cs

    public class NamespaceRoutingConvention : IControllerModelConvention
    {
        private readonly string _baseNamespace;
    
        public NamespaceRoutingConvention(string baseNamespace)
        {
            _baseNamespace = baseNamespace;
        }
    
        public void Apply(ControllerModel controller)
        {
            var hasRouteAttributes = controller.Selectors.Any(selector =>
                selector.AttributeRouteModel != null);
            if (hasRouteAttributes)
            {
                // This controller manually defined some routes, so treat this 
                // as an override and not apply the convention here.
                return;
            }
    
            // Use the namespace and controller name to infer a route for the controller.
            //
            // Example:
            //
            //  controller.ControllerTypeInfo ->    "My.Application.Admin.UsersController"
            //  baseNamespace ->                    "My.Application"
            //
            //  template =>                         "Admin/[controller]"
            //
            // This makes your routes roughly line up with the folder structure of your project.
            //
            if (controller.ControllerType.Namespace == null)
                return;
    
            var template = new StringBuilder(GetControllerNamespace(controller.ControllerType.Namespace));
    
            template.Replace('.', '/');
            template.Append("/[controller]");
    
            foreach (var selector in controller.Selectors)
            {
                selector.AttributeRouteModel = new AttributeRouteModel()
                {
                    Template = template.ToString()
                };
            }
        }
    
        private string GetControllerNamespace(string controllerNamespace)
        {
            return controllerNamespace == _baseNamespace
                ? ""
                : controllerNamespace.Substring(
                    _baseNamespace.Length + 1,
                    controllerNamespace.Length -
                    _baseNamespace.Length - 1);
        }
    }
    

    启动.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options => 
            options.Conventions.Add(new NamespaceRoutingConvention("Enter the route namespace of the api folder")))
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
     }
    

    API文件夹结构

    在api文件夹下,我现在有以下结构:

    /api
      /Ploop
        HelloController.cs
      HelloController.cs
      TestController.cs
    

    样本控制器代码

    因此每个控制器代码如下所示:

    public class HelloController : ControllerBase
    {
        [HttpGet]
        public JsonResult Index()
        {
            return new JsonResult(new
            {
                message = "hello from XXX controller"
            });
        }
    
        [HttpGet("{id?}")]
        public JsonResult Index(int? id)
        {
            return new JsonResult(new
            {
                message = "Hello from XXX controller with index",
                id
            });
        }
    }
    

    呼叫控制器

    因此,当我们调用每个控制器时,我们会在浏览器中得到以下输出:

    api/ploop/hellocontroller.cs版本

    http://localhost:51248/Ploop/Hello

    {"message":"Hello from Ploop HelloController"}
    

    http://localhost:51248/Ploop/Hello/12

    {"message":"Hello from Ploop HelloController with index","id":12}
    

    API/HelloController.cs标准

    http://localhost:51248/Hello

    {"message":"Hello from root HelloController"}
    

    http://localhost:51248/Hello/12

    {"message":"Hello from root HelloController with index","id":12}
    

    API/测试控制器.cs

    http://localhost:51248/Test

    {"message":"Hello from TestController"}
    

    http://localhost:51248/Test/12

    {"message":"Hello from TestController with index","id":12}