Routing problems using Asp.net core

Asked

Viewed 231 times

-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: inserir a descrição da imagem aqui

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

EDIT2 inserir a descrição da imagem aqui

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!

  • 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!

  • Do the following in place of Cpf put id([Route("api/getcliente/{id}")]) and in the parametric int id(public IEnumerable<Cliente> GetCliente(int cpf)) also...

  • Try removing the Route "api" that is on top of your method.

  • 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.

  • How did you do this project? As we noticed has enough problems your code and this can make it not work properly !!!

  • Search for Routes.Maproute within the Startup.Cs class and enter the code here, please.

  • Will need some class attribute in the Route? I tried the Routeprefix and I couldn’t.

  • @Perozzo, the . Net Core doesn’t have it

  • Oh no? I use . Net Core 2.0 and my project has it.

  • @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.

  • @Perozzo, I printed my project and see that I don’t have this folder

  • It is a file and not a folder. Pay attention. It is in front of you the file and you did not see.

  • 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, I edited the post and posted the startup

  • There’s the problem. Your Startup.Cs file is far from where it should be.

  • Got it, missing Maproute, right? I’m looking for an example on how to set up this guy

  • When he created the project from scratch he no longer came with Maproute ready for you?

  • See also the ASP Net Core 2 Razor Pages, that simplifies all of this.

Show 15 more comments

2 answers

0

I did. At Startup I did:

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

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseMvc();
        }

and in the controller I called

[Route("api/[controller]")]
    public class GetClientController : Controller
    {
        LoadClient loadClient = new LoadClient();

        [HttpGet("{cpf}")]
        public IEnumerable<Cliente> GetCliente(Int64 cpf)
        {
            return loadClient.GetCliente(cpf).AsEnumerable().ToList();
        }
    }

With that I solved the problem. Thank you all.

0

In Aspnet Core there is no more Annotation RoutePrefix, now both for Class and Action Annotation Route, like you did.

But when you create the route [Route("api/getcliente/{cpf}")], You’re saying the Cpf property will be received somehow. Added to that, you use a [HttpGet], then you want to pass Cpf by the url itself.

Knowing this, the Postman print shows that you are not stating the url correctly.

She should be: http://localhost:55730/api/getclient/31865881384

If Action still does not receive Cpf, it is because you did not make explicit in the parameter that it should be received via url.

To do so make the following change:

public class GetClientController : Controller
{
    LoadClient loadClient = new LoadClient();

    [HttpGet]
    [Route("api/getcliente/{cpf}")]
    public IEnumerable<Cliente> GetCliente([FromRoute]double cpf)
    {
        return loadClient.GetCliente().AsEnumerable().ToList();
    }
}
  • I have two methods overloaded, 1 with parameter and another without, reason at that time I captured the print of Postman, I was passing the Getall(No parameters), it was bad not to have explained this.

  • I made the changes and keeps returning a statuscode 404(page not found)

  • Actually I got confused, the correct one is Fromroute and not Fromquery. With Fromquery you would need to call the url like this: api/getclient? Cpf=12345678

  • Looking at it well, my controller has a Tolist() for only one element. Can it go well? Because I switched to Fromroute and still didn’t roll

  • I made another change(Edit4) and did not roll

Browser other questions tagged

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