0

Tengo un controlador en mi Web Api llamado ConfigController y dentro un método llamado Index

Si escribo en el navegador http://localhost:5001/api/Config/Index me funciona perfecto,

Pero yo necesito llamarlo de la siguiente forma: http://localhost:5001/Index

¿Cómo puedo hacer esto?

Mi código:

  public class ConfigController : ApiController
{
    private readonly IAppAggregateServices appAggregateServices;

    public ConfigController(IAppAggregateServices appAggregateServices)
    {
        this.appAggregateServices = appAggregateServices;
    }


    [HttpGet]
    public HttpResponseMessage Index()
    {
        string responseString = @"HOLA MUNDO ";
        var response = new HttpResponseMessage();
        response.Content = new StringContent(responseString);
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
        return response;
    }
}

1 respuesta 1

1

Simplemente le agrega la decoración Route al método para cambiarle la ruta:

 [HttpGet]
        [Route("Index")]
        public HttpResponseMessage Index()
        {
            string responseString = @"HOLA MUNDO ";
            var response = new HttpResponseMessage();
            response.Content = new StringContent(responseString);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
            return response;
        }
4
  • Richard Víquez Pérez No funciona el 29 jun. 2020 a las 20:50
  • Es .NET Framework o .NET Core??? el 29 jun. 2020 a las 21:31
  • Es un proyecto .Net Framework el 30 jun. 2020 a las 18:24
  • Yo lo probé con net framework 4.6 como proyecto web api y funcionó!!!!!! Quizás sea por el IAppAggregateServices . Yo lo probé sin esa parte del código puede ser que afecte en algo el 1 jul. 2020 a las 17:38

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.