Problems with routing using asp.net core

-2

When trying to route a core 2.0 application, I can not reach the controller.

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

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

In the above controller, RoutePrefix does not scroll, I tried these two namespace and it did not scroll:

using System.Net.Http;
using System.Web.Http;

In my model I did this

[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 in postman, it does not give me an error, but I do not get the last list. This is the postman:

EDIT1

Imadethiseditionandremoveditfromthemodelanditdoesnotgointothemethod

publicclassGetClientController:Controller{LoadClientloadClient=newLoadClient();[HttpGet][Route("api/getcliente/{cpf}")]
        public IEnumerable<Cliente> GetCliente(Int64 cpf)
        {
            return loadClient.GetCliente(cpf).AsEnumerable().ToList();
        }
    }

EDIT2

EDIT3MyStartap.cs

publicclassStartup{//Thismethodgetscalledbytheruntime.Usethismethodtoaddservicestothecontainer.//Formoreinformationonhowtoconfigureyourapplication,visithttps://go.microsoft.com/fwlink/?LinkID=398940publicvoidConfigureServices(IServiceCollectionservices){}//Thismethodgetscalledbytheruntime.UsethismethodtoconfiguretheHTTPrequestpipeline.publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){if(env.IsDevelopment()){app.UseDeveloperExceptionPage();}app.Run(async(context)=>{awaitcontext.Response.WriteAsync("Hello World!");
            });
        }

EDIT4

I made this change and it still did not roll

[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();
        }
    }
    
asked by anonymous 18.06.2018 / 23:00

2 answers

0

In Aspnet Core there is no longer an annotation RoutePrefix , now for both the annotation of the Class and Action is using Route , as you did.

But when you create the [Route("api/getcliente/{cpf}")] route, you are saying that the cpf property will be received in some way. In addition to that, you use [HttpGet] , so you want to pass cpf through the url itself.

Knowing this, the postman print shows that you are not reporting the url correctly.

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

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

To do this 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();
    }
}
    
19.06.2018 / 03:56
0

I have resolved. In 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 this I solved the problem. Thank you all.

    
19.06.2018 / 18:35