【问题标题】:api call doesnt trigger with postmanapi调用不会用邮递员触发
【发布时间】:2019-11-25 11:57:28
【问题描述】:

我正在尝试获取 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;
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        var connectionString = Configuration["connectionStrings:bookDbConnectionString"];
        services.AddDbContext<BookDbContext>(c => c.UseSqlServer(connectionString));

        services.AddScoped<ICountryRepository, CountryRepository>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BookDbContext context)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();
    }
}

如果我这样做:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BookDbContext context)
{
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        //app.UseRouting();

        app.UseMvc();
}

我收到此警告:

警告 MVC1005
使用端点路由时不支持使用“UseMvc”配置 MVC。要继续使用“UseMvc”,请在“ConfigureServices”中设置“MvcOptions.EnableEndpointRouting = false”。 WebApplication2 D:\Mijn Documents\VisualStudio_2019\WebApplication2\WebApplication2\Startup.cs

【问题讨论】:

  • Configure 中有app.UseMvc() 吗?
  • 冒着听起来非常愚蠢的风险,您是否正在运行代码?您提到您只启动 Visual Studio。
  • ?是的,当然 f5

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


【解决方案1】:

啊,好吧。我看了好几个小时。解决方案是这样的:

public void ConfigureServices(IServiceCollection services)
        {           

            services.AddMvc();

            services.AddControllers();


            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();

            app.UseAuthorization();

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


        }

【讨论】:

  • 请添加一两句话来解释你做了什么。只是发布代码是可怕的。我不得不将它与您的原始代码并排比较,以找出发生了什么变化,只是为了发现这对我的情况没有帮助。
【解决方案2】:

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

[HttpGet]
[Route("api/[controller]")]
public IActionResult GetCountries()

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

[路线("")]

【讨论】:

  • 我更新了帖子
【解决方案3】:

几种不同的方法来实现这一点

[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);

    }
 }

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

【讨论】:

  • 谢谢你我尝试了你的最后一个建议:localhost:51266/api/country 仍然没有
  • 您发布的启动中的Configure 方法缺少app.UseMvc()。添加那个。
  • 你在使用 .NET Core 吗?
  • 是的,我找到了解决方案。我正在使用 .net Core 3。所以是最新版本。
猜你喜欢
  • 2020-04-21
  • 2017-11-23
  • 1970-01-01
  • 2020-03-17
  • 2019-01-20
  • 2021-10-16
  • 2023-02-23
  • 2017-08-17
  • 2019-09-22
相关资源
最近更新 更多