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
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
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.
– Leandro Angelo
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
???– Leandro Angelo
Okay, @Leandroangelo, yes, but it still didn’t happen. I had already put this in and it wasn’t working either.
– pnet
Search and also check the
RegisterRoutes
https://msdn.microsoft.com/en-us/library/cc668201.aspx– Tony
@Leandroangelo, I put other break and nothing and the error in Postman is from Page Not Found(404)
– pnet