调用 api 控制器时,函数的存在就会导致错误

问题描述 投票:0回答:1

我在 asp.net 应用程序中使用 .net 8.0。

我的课程如下:

  /// Controller for diagram retrieval.
  /// </summary>
  [Route("api/Projects")]
  [ApiController]
  [Authorize]
  public class ProjectsEndpoints : ControllerBase
  {
      private IGenesysApiService _apiRequestService;
      private AppSettings _appSettings;

      public ProjectsEndpoints(IGenesysApiService apiClientService, AppSettings appSettings)
      {
          _apiRequestService = apiClientService;
          _appSettings = appSettings;
      }

      public async Task<Project> GetProjects()
      {
          await AddGenesysToken();

          var url = _appSettings.GenesysApiURI + "/Projects";
          var list = await _apiRequestService.GetAsync<Project>(url);

          return list.Body;

      }

      private async Task AddGenesysToken()
      {
          var token = await _apiRequestService.GetAccessTokenForUserAsync();
          _apiRequestService.RequestHeaders.Authorization =
              new AuthenticationHeaderValue("Bearer", token);
      }


  }

如果我添加一个功能,例如:

    public async Task<Entity> GetEntitiesByProject(string projectId)
    {
        try
        {
            await AddGenesysToken();
            var ret = await _apiRequestService.GetAsync<Entity>($"/entities/{projectId}/f5a2162d-74d3-47a0-9b8b-e51d12db78a0");
            return ret.Body;
        }
        catch (Exception ex)
        {
            return null;
        }

    }

我什至没有调用,当尝试调用 getprojects() 时,我收到以下错误。

如果我注释掉未使用的功能,它就可以正常工作。

从缓存加载了 12.51 MB 资源 调试热键:Shift+Alt+D(当应用程序具有焦点时) CSS 热重载忽略 https://localhost:44367/_content/Microsoft.FluentUI.AspNetCore.Components/Microsoft.FluentUI.AspNetCore.Components.bundle.scp.css,因为它无法访问或具有超过 5000 条规则。 挂号的: 失败:Sidekick.Client.Shared.Services.ApiClientService[0] 服务器错误。 失败:Microsoft.AspNetCore.Components.Web.ErrorBoundary[0] System.NullReferenceException:未将对象引用设置为对象的实例。 在 C:\Source\Sidekick\development\Client\Features\Projects\Services\ProjectsService.cs 中的 Sidekick.Client.Features.Projects.Services.ProjectsService.GetProjects() 处:第 23 行 在 C:\Source\Sidekick\development\Client\Features\Diagram\ProjectListComponent.razor 中的 Sidekick.Client.Features.Diagram.ProjectListComponent.OnInitializedAsync() 处:第 51 行 在 Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync() 在Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(任务taskToHandle,ComponentState owningComponentState) System.NullReferenceException:未将对象引用设置为对象的实例。 在 C:\Source\Sidekick\development\Client\Features\Projects\Services\ProjectsService.cs 中的 Sidekick.Client.Features.Projects.Services.ProjectsService.GetProjects() 处:第 23 行 在 C:\Source\Sidekick\development\Client\Features\Diagram\ProjectListComponent.razor 中的 Sidekick.Client.Features.Diagram.ProjectListComponent.OnInitializedAsync() 处:第 51 行 在 Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync() 在 Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(任务 taskToHandle,ComponentState owningComponentState)

我验证了仅当我不向类添加函数时 get 项目才有效。很奇怪。 同样,我所做的唯一更改是将我尚未调用的函数添加到类中。

我正在使用 IIS Express 在调试器中启动。

asp.net api controller .net-8.0
1个回答
0
投票

很奇怪

其实不是。我强烈建议您查看 ASP.NET Core 中的控制器操作路由 文档。基本上,您已经通过以下方式为控制器定义了一条路线:

[Route("api/Projects")]

从之前链接的文档中:

动作定义

控制器上的公共方法(除了具有

NonAction
属性的方法之外)都是操作。

因此,当您添加第二个方法时,您最终会得到两个匹配相同路由的操作,因此,如果您尝试直接调用它,您将得到如下所示的内容:

An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints. Matches:

ProjectsEndpoints.GetProjects (ASPNET8TestApp)
ProjectsEndpoints.GetEntitiesByProject (ASPNET8TestApp)

您可以通过为其提供单独的路线来修复它,例如:

[Route("api/Projects")]
[ApiController]
public class ProjectsEndpoints : ControllerBase
{
    [HttpGet]
    public async Task<Project> GetProjects()
    {
        // ...
    }

    [HttpGet("{projectId}/entity")]
    public async Task<Entity> GetEntitiesByProject(string projectId)
    {
        // ...
    }
}

这将使

GetEntitiesByProject
在路径
api/Projects/{someProjIdHere}/entity

可用
© www.soinside.com 2019 - 2024. All rights reserved.