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



Background

在大多数MVCWebApi应用程序中,我们通常看到以下结构:

/Controllers
HomeController.cs
/Models

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

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

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

增加复杂性

当我们向 Web 应用程序添加其他部分(如自定义操作结果、筛选器和区域等(时,它确实开始变得复杂。 突然之间,顶级文件夹即使组织得很好,也会感觉有点混乱。

问题

通常,当我们添加开箱即用的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

您可以在以下代码中路由类似完成的内容

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

请在此处查看文档

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

来自 Mads 提供的有关控制器路由的部分(请参阅此处 - 自定义路由(

命名空间路由约定.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/testController.cs

http://localhost:51248/Test

{"message":"Hello from TestController"}

http://localhost:51248/Test/12

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

最新更新