0
I’m having trouble testing my end-points with the post and Asp.net core 2.1.
Controller Base
public class ControladorBase : Controller
{
protected IMapper _Mapper;
public ControladorBase()
{
}
public ControladorBase(IMapper mapper)
{
_Mapper = mapper;
}
}
Controller
[Produces("application/json")]
[Route("api/Cartao")]
public class CartaoController : ControladorBase
{
ICartaoBusiness _cartaoBusiness;
public CartaoController(ICartaoBusiness cartaoBusiness)
{
_cartaoBusiness = cartaoBusiness;
}
[HttpPost]
[Route("api/Cartao/AddOrUpdate")]
public IActionResult AddOrUpdate([FromBody]CartaoModel model)
{
if (ModelState.IsValid)
{
Cartao cartao = _Mapper.Map<CartaoModel, Cartao>(model);
if (model.CartaoId.HasValue)
{
_cartaoBusiness.Update(cartao);
}
{
_cartaoBusiness.Insert(cartao);
}
return new JsonResult(new { Success = true });
}
else
{
return new JsonResult(new { Success = false });
}
}
[HttpPost]
[Route("api/Cartao/Teste")]
public IActionResult Teste()
{
return new JsonResult(new { Success = false });
}
}
Startup
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AberturaDeContasContext>(
options =>
options.UseSqlServer(
@"Data Source=3Y1S0L2\SQLEXPRESS;Initial Catalog=AberturaDeContas;User ID=sa;Password=abc102030!;"
));
services.AddTransient<ICartaoBusiness, CartaoBusiness>();
services.AddTransient<IProspectBusiness, ProspectBusiness>();
services.AddAutoMapper();
services.AddMvc();
// Configurando o serviço de documentação do Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new Info
{
Title = "Abertura de Contas Digital",
Version = "v1",
Description = "API REST criada com o ASP.NET Core",
Contact = new Contact
{
Name = "Kaio Nylander",
}
});
string caminhoAplicacao =
PlatformServices.Default.Application.ApplicationBasePath;
string nomeAplicacao =
PlatformServices.Default.Application.ApplicationName;
//string caminhoXmlDoc =
// Path.Combine(caminhoAplicacao, $"{nomeAplicacao}.xml");
//c.IncludeXmlComments(caminhoXmlDoc);
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
// Ativando middlewares para uso do Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json",
"Abertura de Contas Digital");
});
}
}
Program
public class Program
{
public static void Main(string[] args)
{
var webHost = BuildWebHost(args);
using (var scope = webHost.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var db = services.GetRequiredService<AberturaDeContasContext>();
db.Database.Migrate();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while migrating the database.");
}
}
webHost.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://*:5051")
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
}
Error in call
You are sending the POST by Postman using which Body tab option?
– TonCunha