-2
When trying to route a core 2.0 application, I can’t get to the controller.
public class GetClientController : Controller
{
LoadClient loadClient = new LoadClient();
[HttpGet]
public IEnumerable<Cliente> GetCliente(double cpf)
{
return loadClient.GetCliente().AsEnumerable().ToList();
}
}
In the controller above, the Routeprefix does not roll, I tried these two namespace and did not roll:
using System.Net.Http;
using System.Web.Http;
On my model I did it
[Route("api/getcliente/{cpf}")]
public List<Cliente> GetCliente(Int64 cpf)
{
List<Cliente> cliente = new List<Cliente>();
cliente.Add(new Cliente(123456, "[email protected]", "986754007", "CB", "EML"));
cliente.Add(new Cliente(908734, "[email protected]", "988877731", "CB", "SMS"));
cliente.Add(new Cliente(674300, "[email protected]", "965241131", "PF", "EML"));
cliente.Add(new Cliente(101654, "[email protected]", "987450101", "EX", "EML"));
cliente.Add(new Cliente(501274, "[email protected]", "986754144", "PA", "SMS"));
var lista = cliente
.Where(cp => cp.Cpf == cpf).ToList();
return lista;
}
when I test on Postman, it doesn’t make a mistake, but I don’t get the list passed. This is Postman:
EDIT1
I made this edition and removed from the model and still does not enter the method
public class GetClientController : Controller
{
LoadClient loadClient = new LoadClient();
[HttpGet]
[Route("api/getcliente/{cpf}")]
public IEnumerable<Cliente> GetCliente(Int64 cpf)
{
return loadClient.GetCliente(cpf).AsEnumerable().ToList();
}
}
EDIT3 My Startap.Cs
public class Startup
{
// 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)
{
}
// 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.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
EDIT4
I made that change and it still didn’t happen
[Route("api/[controller]")]
public class GetClientController : Controller
{
LoadClient loadClient = new LoadClient();
[HttpGet("getcliente/{cpf}")]
public IEnumerable<Cliente> GetCliente(Int64 cpf)
{
return loadClient.GetCliente(cpf).AsEnumerable().ToList();
}
}
Did you put routing into the model? routing is in the Controller class!
– novic
I don’t know what you did, just know that in your question the route is in the wrong place has to be in controller ! and also the route is in trouble!
– novic
Do the following in place of Cpf put
id
([Route("api/getcliente/{id}")]
) and in the parametricint id
(public IEnumerable<Cliente> GetCliente(int cpf)
) also...– novic
Try removing the Route "api" that is on top of your method.
– perozzo
If your Getclient method expects a parameter, you have to pass a parameter to it, otherwise you won’t find anything at all. Note that in your Powerpoint, the method you called is getclient and not getclient.
– perozzo
How did you do this project? As we noticed has enough problems your code and this can make it not work properly !!!
– novic
Search for Routes.Maproute within the Startup.Cs class and enter the code here, please.
– perozzo
Will need some class attribute in the Route? I tried the Routeprefix and I couldn’t.
– pnet
@Perozzo, the . Net Core doesn’t have it
– pnet
Oh no? I use . Net Core 2.0 and my project has it.
– perozzo
@Virgilionovic, this project is just a mock to test another service. It is not just that and for testing. I had to do it in a hurry and it did not function.
– pnet
@Perozzo, I printed my project and see that I don’t have this folder
– pnet
It is a file and not a folder. Pay attention. It is in front of you the file and you did not see.
– perozzo
Let’s go continue this discussion in chat.
– perozzo
Did you read what I said? Let’s go again: Search for Routes.Maproute within the Startup.Cs class and put the code here, please.
– perozzo
@Perozzo, I edited the post and posted the startup
– pnet
There’s the problem. Your Startup.Cs file is far from where it should be.
– perozzo
Got it, missing Maproute, right? I’m looking for an example on how to set up this guy
– pnet
When he created the project from scratch he no longer came with Maproute ready for you?
– perozzo
See also the ASP Net Core 2 Razor Pages, that simplifies all of this.
– Tony