.Net Api 无法识别我的控制器

.Net Api doesn't recognize my Controllers

伙计们,我有一个简单的后端代码,我只是想集成 swagger ui,但它只能识别 6 中的 3 个控制器和 7 中的 3 个模式,但是当我尝试向邮递员发出获取请求时,它给出了 http 404 错误。我做错了什么? 我在 Visual Studio 2019 中使用 Win11 和 .net 5。

startup.cs

public void ConfigureServices(IServiceCollection services)
    {

        services.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "SwaggerApi", Version = "v1" });
        });
        services.AddCors();
        services.AddHttpContextAccessor();
        services.AddSwaggerGen();

        services.AddDependencyResolvers(new ICoreModule[] {
            new CoreModule()
        });
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }


        app.UseCors(builder => builder.WithOrigins("http://localhost:5000").AllowAnyHeader());

        app.UseHttpsRedirection();

        app.UseWebSockets();

        app.UseRouting();

        app.UseAuthentication();

        app.UseStaticFiles();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

        app.UseSwagger();

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample API");
        });
    

program.cs

public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureContainer<ContainerBuilder>(builder =>
            {
                builder.RegisterModule(new AutofacBusinessModule());
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

编辑:我没有放置我的控制器

这是公认的控制器样本 usersController.cs

[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
    private IUserService _userService;

    public UsersController(IUserService userService)
    {
        _userService = userService;
    }


    [HttpGet("getall")]
    public IActionResult Get()
    {

        var result = _userService.GetAll();
        if (result.Success)
        {
            return Ok(result.Data);
        }
        return BadRequest(result.Message);

    }

    [HttpGet("getbyid")]
    public IActionResult Get(int id)
    {
        var result = _userService.GetById(id);
        if (result.Success)
        {
            return Ok(result);
        }
        return BadRequest(result.Message);
    }

    




    [HttpPost("add")]
    public IActionResult PostAdd(User user)
    {
        var result = _userService.Add(user);
        if (result.Success)
        {
            return Ok(result.Message);
        }
        return BadRequest(result.Message);


    }


    [HttpPost("update")]
    public IActionResult PostUpdate(User user)
    {
        var result = _userService.Update(user);
        if (result.Success)
        {
            return Ok(result);
        }
        return BadRequest(result.Message);


    }


    [HttpPost("delete")]
    public IActionResult PostDelete(User user)
    {
        var result = _userService.Delete(user);
        if (result.Success)
        {
            return Ok(result);
        }
        return BadRequest(result.Message);


    }
}

此样本无法识别

systemStavesController.cs

[Route("api/[controller]")]
[ApiController]
public class SystemStavesController : ControllerBase
{
    private ISystemStaffService _staffService;

    public SystemStavesController(ISystemStaffService staffService)
    {
        _staffService = staffService;
    }
    
    [HttpGet("getall")]
    IActionResult GetAll()
    {
        var result = _staffService.GetAll();
        if (result.Success)
        {
            return Ok(result.Data);
        }

        return BadRequest(result.Message);
    }

    [HttpGet("getbyid")]
    IActionResult GetById(int id)
    {
        var result = _staffService.GetById(id);
        if (result.Success)
        {
            return Ok(result.Data);
        }

        return BadRequest(result.Message);
    }

    [HttpPost("add")]
    IActionResult Add(SystemStaff staff)
    {
        var result = _staffService.Add(staff);
        if (result.Success)
        {
            return Ok(result.Message);
        }

        return BadRequest(result.Message);
    }

    [HttpPost("delete")]
    IActionResult Delete(SystemStaff staff)
    {
        var result = _staffService.Delete(staff);
        if (result.Success)
        {
            return Ok(result.Message);
        }

        return BadRequest(result.Message);
    }


    [HttpPost("update")]
    IActionResult Update(SystemStaff staff)
    {
        var result = _staffService.Update(staff);
        if (result.Success)
        {
            return Ok(result.Message);
        }

        return BadRequest(result.Message);
    }
}

我能注意到控制器工作和不工作的一个区别是 SytemStavesController 没有任何方法的访问修饰符,而 UsersController 有所有具有 public 访问权限的已声明方法。可能是允许您访问控制器端点的限制级别。