ASP核心:如何路由到位于区域文件夹中的API控制器?
ASP Core: how to route to API Controller that is located in the area folder?
结构:
+ MyProj
+ Areas
+ Configuration
- Pages
- ConfigurationApiController.cs
创建不带 Controllers 文件夹的控制器是由 VS2017 提出的,这对我来说没问题,因为我使用 Razor Pages 并且不需要 Controllers 文件夹:
那些不起作用:
- http://localhost:8080/api/Users
- http://localhost:8080/api/GetUsers
- http://localhost:8080/Configuration/api/Users
- http://localhost:8080/Configuration/api/GetUsers
控制器定义:
[Route("api")]
[Produces("application/json")]
[ApiController]
public class ConfigurationApiController : ControllerBase
{
private readonly ApplicationSettings applicationSettings;
[HttpGet]
public ActionResult GetUsers()
{
Mvc路由配置标准方式:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
如何路由到 ConfigurationApiController
的 GetUsers
操作?
修改api路由,添加区域属性,为[area]路由提供区域名称。
[Area("Configuration")]
[Route("[area]/api/[controller]")]
[ApiController]
public class ConfigurationApiController : ControllerBase
{
}
仅此而已,可以在http://localhost:8080/Configuration/api/ConfigurationApi
访问
其他一些路由选项:
使用AspNetCore.RouteAnalyzer working option found: http://localhost:8080/api(是的,没有操作)
删除 Web API 属性后
// [Route("api")]
// [Produces("application/json")]
// [ApiController]
然后 http://localhost:8080/ConfigurationApi/GetUsers
可能没问题,但路由中没有区域,似乎 "routing to the area by conventions" 不起作用是 asp 核心:
ASP Core: how to configure area for api controller without AreaAttribute (or how to enable convention area routing for Api controller)?
和 https://github.com/aspnet/AspNetCore/issues/7042
同样在这种情况下 ContentResult { Content = json, ContentType = "application/json" }
应该是 return 但这对我来说没问题,因为我更喜欢使用就地序列化而不是流序列化程序。
[Area("Configuration")]
[Route("[area]/api")]
[Produces("application/json")]
[ApiController]
其他选项:[Route([area]/api/[action]")]路由到http://localhost:8080/Configuration/api/GetUsers
删除区域属性时抛出 运行 时间错误 Error: While processing template '[area]/api', a replacement value for the token 'area' could not be found. Available tokens: 'action, controller'. To use a '[' or ']' as a literal string in a route or within a constraint, use '[[' or ']]' instead.
//[Area("Configuration")]
[Route("[area]/api")]
[Produces("application/json")]
[ApiController]
要支持@Url.Action(action: "myArea", controller: "myControllerApi")路由需要手动配置。
Asp核心路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "defaultArea",
template: "{area:exists}/{controller}/{action}"); // matches only those where area route value is defined
});
Asp核心3路由(启动Configure):
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "defaultArea",
pattern: "{area:exists}/{controller}/{action}");
});
结构:
+ MyProj
+ Areas
+ Configuration
- Pages
- ConfigurationApiController.cs
创建不带 Controllers 文件夹的控制器是由 VS2017 提出的,这对我来说没问题,因为我使用 Razor Pages 并且不需要 Controllers 文件夹:
那些不起作用:
- http://localhost:8080/api/Users
- http://localhost:8080/api/GetUsers
- http://localhost:8080/Configuration/api/Users
- http://localhost:8080/Configuration/api/GetUsers
控制器定义:
[Route("api")]
[Produces("application/json")]
[ApiController]
public class ConfigurationApiController : ControllerBase
{
private readonly ApplicationSettings applicationSettings;
[HttpGet]
public ActionResult GetUsers()
{
Mvc路由配置标准方式:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
如何路由到 ConfigurationApiController
的 GetUsers
操作?
修改api路由,添加区域属性,为[area]路由提供区域名称。
[Area("Configuration")]
[Route("[area]/api/[controller]")]
[ApiController]
public class ConfigurationApiController : ControllerBase
{
}
仅此而已,可以在http://localhost:8080/Configuration/api/ConfigurationApi
访问其他一些路由选项:
使用AspNetCore.RouteAnalyzer working option found: http://localhost:8080/api(是的,没有操作)
删除 Web API 属性后
// [Route("api")]
// [Produces("application/json")]
// [ApiController]
然后 http://localhost:8080/ConfigurationApi/GetUsers
可能没问题,但路由中没有区域,似乎 "routing to the area by conventions" 不起作用是 asp 核心: ASP Core: how to configure area for api controller without AreaAttribute (or how to enable convention area routing for Api controller)? 和 https://github.com/aspnet/AspNetCore/issues/7042
同样在这种情况下 ContentResult { Content = json, ContentType = "application/json" }
应该是 return 但这对我来说没问题,因为我更喜欢使用就地序列化而不是流序列化程序。
[Area("Configuration")]
[Route("[area]/api")]
[Produces("application/json")]
[ApiController]
其他选项:[Route([area]/api/[action]")]路由到http://localhost:8080/Configuration/api/GetUsers
删除区域属性时抛出 运行 时间错误 Error: While processing template '[area]/api', a replacement value for the token 'area' could not be found. Available tokens: 'action, controller'. To use a '[' or ']' as a literal string in a route or within a constraint, use '[[' or ']]' instead.
//[Area("Configuration")]
[Route("[area]/api")]
[Produces("application/json")]
[ApiController]
要支持@Url.Action(action: "myArea", controller: "myControllerApi")路由需要手动配置。
Asp核心路线:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "defaultArea",
template: "{area:exists}/{controller}/{action}"); // matches only those where area route value is defined
});
Asp核心3路由(启动Configure):
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute(
name: "defaultArea",
pattern: "{area:exists}/{controller}/{action}");
});