Configure API by Action MVC5

2

I'm having trouble accessing my WebApi, maybe it's a setup issue, I'm not sure what I'm doing wrong.

When I try to access my api via ajax, I get a 404 (not found) error.

I had an MVC5 project, in this project I created a folder called WebServices where I created WebApi2 files with various actions. Ex.:

public class ClienteController : ApiController
{

    [Route("api/cliente/lista")]
    [HttpPost]
    public IHttpActionResult Lista(long? idCliente)
    {
        try
        {
            return Ok(_clienteRepository.DropDownList(idCliente));
        }
        catch (Exception ex)
        {
            return ResponseMessage(
                Request.CreateErrorResponse(
                    HttpStatusCode.InternalServerError, "Ops! Ocorreu um problema interno. \n"
                    + ex.Message.ToString()));
        }
    }

}

To call this I am making the following ajax request: Ex.:

function listarCliente() {
    $.ajax({
        url: "../../api/cliente/lista", //Aqui eu volto, pois estou dentro de uma Area.
        data: {
            idCliente
        },
        type: "POST",
        success: function (value) {
            ...
        }
    })  
}

My WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    //Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
         name: "WithActionApi",
         routeTemplate: "api/{controller}/{action}"
    );

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}
    
asked by anonymous 11.09.2018 / 13:51

0 answers