Post method is not running on Postman

Asked

Viewed 777 times

0

Creating a method POST and testing by Postman, I can’t access the Controller that POST. I haven’t implemented much yet, but I should stop at Break if, of course, it was working. Controller

[Route("api/[controller]")]
public class OptOutClientController : Controller
{
    private IOptOutCreateService optOutCreateService;

    HttpClient httpClient = new HttpClient();

    public OptOutClientController(IOptOutCreateService optOutCreateService)
    {
        this.optOutCreateService = optOutCreateService;
    }

    [HttpPost]
    public async Task<IActionResult> OptOutPostClient([FromBody]OptOutRequest client)
    { **Breakpoint nessa chave e não entra **

        if (client == null)                                                                                          
            throw new OptOutException( "Favor informar os dados do OptOut!");

        var result = await optOutCreateService.Process(new OptOutCreateCommand(client.Cpf, client.Email, client.Telefone, client.Bandeira, client.Canal));

        return Ok(new ApiReturnItem<OptOutResult> { Item = result, Success = true });
    }
}

Screenshot of Postman running the method POST inserir a descrição da imagem aqui

EDIT1

I made a small example with nothing, just a controller and nothing else and I have the same error: Startup.Cs

public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddRouting();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}");
            });
        }
    }

controller

[Route("api/[controller]")]
    public class HomeController : Controller
    {
        [HttpPost]
        public IActionResult postar()
        {
            return Ok();
        }
    }

Postman url:http://localhost:51553/api/home/post body: {"test": "1"}

And get the same page not found error

  • 1

    put other breakpoints, your post is going to the API and returning an error 500, which represents a failure on the server side. Maybe the Exception is occurring before you even enter your action.

  • And check the routes, noting that you are using [Route("api/[controller]")] and your post is only for /api/OptOutClient, shouldn’t be for /api/OptOutClient/OptOutPostClient ???

  • Okay, @Leandroangelo, yes, but it still didn’t happen. I had already put this in and it wasn’t working either.

  • Search and also check the RegisterRoutes https://msdn.microsoft.com/en-us/library/cc668201.aspx

  • @Leandroangelo, I put other break and nothing and the error in Postman is from Page Not Found(404)

1 answer

1


Explanation

You’ve assigned a route to Controller but did not assign to the Action, in this case, the Conventional Routing. In this type of routing, by default, the name of the Action for the construction of the route.

In your case, you are not entering the section of the route responsible for directing the Action (OptOutPostClient)

Solution

Change the POST directing to the route below:

http://localhost:51585/api/OptOutClient/OptOutPostClient

If you do not have a default route applied, put as follows:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}");
        });
  • or simply rename the method OptOutPostClient for Post, if there are no other action using the same verb

  • Jean, good morning. Even though I put Action’s name on Postman, I’m still not getting to Break

  • Is build set to Debug or Release? If it’s Release, switch to Debug.

  • @Jeangustavoprates, Debug and on my Startup.Cs I have already set up to accept routes: services.AddMvc();&#xA;services.AddRouting(); and also app.UseMvc();

  • I updated the answer

  • @Jeangustavoprates, I continue with 404, but in those words (I forgot the name) above the class statement, says I got X requests, before I was with 0 requests.

  • pnet, try to take the [Route("api/[controller attribute]")]

  • @Leandroangelo, I marked your answer, but it only works without Action in the URL, even if mapping the route in Startup. If you can edit this, thank you.

  • Just to try to solve this problem, everything happens of error 500 is when I inject a dependency into the Controller constructor.

  • You configured the implementation in Startup.Cs?

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.