ASP.NET Core 2.2 Web API未命中任何已配置的路由,这是为什么?

时间:2019-02-08 13:08:55

标签: c# asp.net-web-api asp.net-core .net-core

我创建了看起来像这样的BaseController

`[ApiController]
  public class MoviesPlaceBaseController : ControllerBase
  {
    protected readonly IMoviesPlaceSupervisor _moviesPlaceSupervisor;

    public MoviesPlaceBaseController(IMoviesPlaceSupervisor moviesPlaceSupervisor)    
    {
        _moviesPlaceSupervisor = moviesPlaceSupervisor;
    }`

在我的“ PostController”中,我从看起来像这样的基本控制器类中获得

`    [Route("[controller]")]
    [Produces("application/json")]
    public class PostController : MoviesPlaceBaseController
    {
      public PostController(IMoviesPlaceSupervisor supervisor) : base (supervisor){ }

        // GET api/post
        [HttpGet]
        [Produces(typeof(List<PostViewModel>))]
        public async Task<ActionResult<List<PostViewModel>>> Get(CancellationToken ct = default(CancellationToken))
        {
          return new ObjectResult(await _moviesPlaceSupervisor.GetAllPostsAsync(ct));
        }`

我的launchSettings.json我删除了https网址,现在看起来像这样

`"MoviesPlaceAPI": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/",
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }`

我的Startup.cs ConfigureServices如下

` public void ConfigureServices(IServiceCollection services)
    {
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

      services.AddConnectionProvider(Configuration)
        .ConfigureSupervisor()
        .AddMiddleware()
        .AddCorsConfiguration()
        .ConfigureRepositories();
    }`

运行应用程序时,我无法点击api / post路由。当我在该操作中设置断点并运行该应用程序时,断点会显示其“未验证”。我尝试做api / post和api / posts。在控制器类中,我删除了仅带有控制器“ post”名称的Route [“ controller”],但同样。奇怪的是,即使该控制器不再存在于我的应用程序中,我也可以很好地命中api / values。我不知道这是怎么回事,有人可以请大家解释一下。

1 个答案:

答案 0 :(得分:1)

我将项目从asp.net core 2.1升级到2.2。它并没有完全摆脱以前编译的代码。在将其更改为2.2之后,我的launch.json文件仍然指向Debug / netcoreapp.2.1...。