简体   繁体   English

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

[英]ASP.NET Core 2.2 Web API not hitting any of the configured routes, why is that?

I have created BaseController that looks like this我创建了如下所示的 BaseController

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

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

In my "PostController" I derive from that base controller class which looks like this在我的“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));
        }`

My launchSettings.json I removed the https url and it now looks like this我的 launchSettings.json 我删除了 https url,现在看起来像这样

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

my Startup.cs ConfigureServices looks like this我的 Startup.cs ConfigureServices 看起来像这样

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

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

When running my application I cannot hit my api/post route.运行我的应用程序时,我无法访问我的 api/post 路由。 When I put break points in that action and run the app, the breakpoint says its "unverified".当我在该操作中放置断点并运行该应用程序时,断点显示其“未验证”。 I tried doing api/post and api/posts.我尝试做 api/post 和 api/posts。 In the controller class i removed the Route["controller"] with just the name of the controller "post", but same thing.在控制器类中,我删除了 Route["controller"] 控制器的名称“post”,但同样的事情。 The weird thing is that I can hit api/values just fine, even though that controller no longer exists in my app.奇怪的是,即使该控制器不再存在于我的应用程序中,我也可以很好地点击 api/values。 I have no idea what is going on, can someone shed some light please.我不知道发生了什么,有人可以透露一些信息吗?

I upgraded my project from asp.net core 2.1 to 2.2 When I performed clean then build.当我执行 clean 然后构建时,我将我的项目从 asp.net core 2.1 升级到 2.2。 It did not entirely get rid of the previously compiled code.它并没有完全摆脱以前编译的代码。 My launch.json file was still pointing to Debug/netcoreapp.2.1... after changing that to 2.2 everything started working as expected.我的 launch.json 文件仍然指向 Debug/netcoreapp.2.1... 将其更改为 2.2 后,一切都按预期开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM