为了账号安全,请及时绑定邮箱和手机立即绑定

为什么我的 API 调用没有通过邮递员触发? MVC1005警告

为什么我的 API 调用没有通过邮递员触发? MVC1005警告

C#
紫衣仙女 2023-12-17 10:30:30
我正在尝试进行 API 调用;我启动 Visual Studio 并在邮递员中输入:http://localhost:51266/api/country我在该方法上设置了断点,但没有任何反应。我得到了 404 未找到。这是控制器:[Route("api/[controller]")][ApiController]public class CountryController : Controller{    private ICountryRepository countryRepository;    public CountryController(ICountryRepository repository)    {        this.countryRepository = repository;    }    [HttpGet]    public IActionResult GetCountries()    {        var countries = countryRepository.GetCountries().ToList();        return Ok(countries);    }}我在这里做错了什么?我在 Startup 中有这个:public void ConfigureServices(IServiceCollection services){    services.AddMvc();    var connectionString = Configuration["connectionStrings:bookDbConnectionString"];    services.AddDbContext<BookDbContext>(c => c.UseSqlServer(connectionString));    services.AddScoped<ICountryRepository, CountryRepository>();}我现在有这样的:[ApiController]public class CountryController : Controller{    private ICountryRepository countryRepository;    public CountryController(ICountryRepository repository)    {        this.countryRepository = repository;    }    [HttpGet]    [Route("api/[controller]")]    public IActionResult GetCountries()    {        var countries = countryRepository.GetCountries().ToList();        return Ok(countries);    }}和我的启动课程:public class Startup{    public static IConfiguration Configuration { get; set; }    public Startup(IConfiguration configuration)    {        Configuration = configuration;    }我收到这个警告:警告 MVC1005使用“UseMvc”使用端点路由时不支持配置 MVC。要继续使用“UseMvc”,请设置“MvcOptions.EnableEndpointRouting = false”在“配置服务”内。 WebApplication2 D:\Mijn Documents\VisualStudio_2019\WebApplication2\WebApplication2\Startup.cs
查看完整描述

3 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

解决方案是这样的:


public void ConfigureServices(IServiceCollection services) {           

    services.AddMvc();

    services.AddControllers(); //added this


    var connectionString = Configuration["connectionStrings:bookDbConnectionString"];


    services.AddDbContext<BookDbContext>(c => c.UseSqlServer(connectionString));

    services.AddScoped<ICountryRepository, CountryRepository>();

}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BookDbContext context) {


    if (env.IsDevelopment()) {

        app.UseDeveloperExceptionPage();

    }

  

    app.UseRouting(); //uncommented 

    app.UseAuthorization(); //added this

    app.UseEndpoints(endpoints => { //added this

        endpoints.MapControllers();

    });


    //removed the app.UseMvc(); line

}


查看完整回答
反对 回复 2023-12-17
?
繁花如伊

TA贡献2012条经验 获得超12个赞

结合不同的方法来实现这一目标


[Route("api/country")]

[ApiController]

public class CountryController : Controller

{


    private ICountryRepository countryRepository;


    public CountryController(ICountryRepository repository)

    {

        this.countryRepository = repository;

    }


    [HttpGet]

    public IActionResult GetCountries()

    {


        var countries = countryRepository.GetCountries().ToList();


        return Ok(countries);


    }

 }

或者


[Route("api/[controller]")]

[ApiController]

public class CountryController : Controller

{


    private ICountryRepository countryRepository;


    public CountryController(ICountryRepository repository)

    {

        this.countryRepository = repository;

    }


    [HttpGet("")]

    public IActionResult GetCountries()

    {


        var countries = countryRepository.GetCountries().ToList();


        return Ok(countries);


    }

 }

我最喜欢这个,我认为它是最直观的


[Route("api")]

[ApiController]

public class CountryController : Controller

{


    private ICountryRepository countryRepository;


    public CountryController(ICountryRepository repository)

    {

        this.countryRepository = repository;

    }


    [HttpGet("country")]

    public IActionResult GetCountries()

    {


        var countries = countryRepository.GetCountries().ToList();


        return Ok(countries);


    }

 }

如果这些都不起作用,那是因为您没有在 中的 方法中调用 app.UseMVcConfigureStartup.cs


查看完整回答
反对 回复 2023-12-17
?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

问题是路线,必须继续行动


[HttpGet]

[Route("api/[controller]")]

public IActionResult GetCountries()

或者,您可以将路由保留在控制器上,然后将一个空路由添加到操作中:


[Route("")]


查看完整回答
反对 回复 2023-12-17
  • 3 回答
  • 0 关注
  • 63 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信