joeyanthon
joeyanthon

Reputation: 218

How to start a webApi Asp.net

I'm trying to start an ASP.NET Web API which I created but page always appears not found.

My controller:

using System;
using System.Collections.Generic;
using System.Linq;
using CrudAPI.ViewModel;
using Microsoft.Extensions.Logging;
using System.Net;
using System.Net.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

namespace CrudAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ClienteController : ControllerBase
    {
        private readonly ILogger<ClienteController> _logger;

        public ClienteController(ILogger<ClienteController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        [Route("ListarClientes")]
        public List<Cliente> ListarClientes()
        {
            return new List<Cliente>();
        }
    }
}

I click start from Visual Studio with IIS Express and it open this link:

   https://localhost:44395/cliente

I also tried to start a page through port 5000 and 5001 - but I keep getting

404 not found.

Upvotes: 0

Views: 44

Answers (1)

RonCG
RonCG

Reputation: 369

Your url is incorrect, there is no route defined for it. When you set this: [Route("api/[controller]")], it means that all the routes of the controller will start with "api/cliente". So, to access your get route, you have to call: https://localhost:44395/api/cliente/listarclientes

Upvotes: 1

Related Questions