亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的 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 關注
  • 205 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號