gpt4 book ai didi

asp.net-core - ASP 核心 : how to route to API Controller that is located in the area folder?

转载 作者:行者123 更新时间:2023-12-03 18:35:44 25 4
gpt4 key购买 nike

结构:

+ MyProj
+ Areas
+ Configuration
- Pages
- ConfigurationApiController.cs

创建没有 Controllers 文件夹的 Controller 是由 VS2017 提出的,对我来说没问题,因为我使用 Razor Pages 并且不需要 Controllers 文件夹:

enter image description here

那些不起作用:
  • http://localhost:8080/api/Users
  • http://localhost:8080/api/GetUsers
  • http://localhost:8080/Configuration/api/Users
  • http://localhost:8080/Configuration/api/GetUsers

  • Controller 定义:

    [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?}");
    });

    如何路由到 GetUsers ConfigurationApiController的 Action ?

    最佳答案

    修改 api 路由并添加区域属性以提供 [area] 路由的区域名称。

        [Area("Configuration")]
    [Route("[area]/api/[controller]")]
    [ApiController]
    public class ConfigurationApiController : ControllerBase
    {
    }

    就这些,可以访问 http://localhost:8080/Configuration/api/ConfigurationApi

    关于asp.net-core - ASP 核心 : how to route to API Controller that is located in the area folder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375351/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com