It is possible to have multiple search methods in the same WEB API control

0

For example, A method that searches according to the received in the "schedule" parameter of the method:

[ResponseType(typeof(HORARIODOCENTE))]
        public IHttpActionResult GetHorariosDocente([FromUri] HORARIODOCENTE horario)
        {
            try
            {
                List<HORARIODOCENTE> lstHorariosDocente = LGHorarioDocente.GetHorarioDocente();
                List<HORARIODOCENTE> HorariosDocente = lstHorariosDocente.FindAll(x => x.IdCicloEscolar == horario.IdCicloEscolar && x.IdDocente == horario.IdDocente && x.Dia == horario.Dia && x.HoraInicio == horario.HoraInicio);

                if (HorariosDocente == null)
                {
                    return NotFound();
                }
                return Ok(HorariosDocente);
            }
            catch
            {
                return InternalServerError();
            }
        }

and another with "teacher" parameter

[ResponseType(typeof(HORARIODOCENTE))]
            public IHttpActionResult GetDocenteId([FromUri] HORARIODOCENTE docente)
            {
                try
                {
                    List<HORARIODOCENTE> lstHorariosDocente = LGHorarioDocente.GetHorarioDocente();
                    List<HORARIODOCENTE> Docente= lstHorariosDocente.FindAll(x => x.IdCicloEscolar == docente.IdCicloEscolar && x.IdDocente == docente.IdDocente);

                    if (Docente== null)
                    {
                        return NotFound();
                    }
                    return Ok(HorariosDocente);
                }
                catch
                {
                    return InternalServerError();
                }
            }
    
asked by José MN 30.11.2016 в 21:04
source

2 answers

1

Decorate the endpoints of your controller with the Route attribute, as well as the driver with the RoutePrefix attribute:

[RoutePrefix("api/ValidaHoraInicio")] // Prefijo para llamar al controlador
public class ValidarHoraInicioController : ApiController
{
    [HttpGet] // Indica el verbo HTTP utilizado por el endpoint
    [Route("GetHorariosDocente")] // Indica el nombre de la acción
    [ResponseType(typeof(HORARIODOCENTE))]
    public IHttpActionResult GetHorariosDocente([FromUri] HORARIODOCENTE horario)
    {
        try
        {
            // Código omitido por brevedad...
        }
        catch
        {
            return InternalServerError();
        }
    }

    [HttpGet]
    [Route("GetDocenteId")]
    [ResponseType(typeof(HORARIODOCENTE))]
    public IHttpActionResult GetDocenteId([FromUri] HORARIODOCENTE docente)
    {
        try
        {
            // Código omitido por brevedad...
        }
        catch
        {
            return InternalServerError();
        }
    }
}

With this, the URIs for your AJAX requests would look like this:

  • [domain]: [port] / api / ValidaHoraInicio / GetHorariosDocente
  • [domain]: [port] / api / ValidaHoraInicio / GetDocenteId

and so you can perform different search methods on the same controller.

    
answered by 03.02.2017 / 21:03
source
0

The problem is not with the methods in your Controller , it basically corresponds to a error of Google Chrome

You can fix it by adding it to your hosts file. If you use Windows you can find it in C: \ Windows \ System32 \ drivers \ etc . The line to add is:

127.0.0.1 localhost yourdomain.com

For more information you can consult the following OS question in English

    
answered by 30.11.2016 в 23:51