Map Api port in an MVC project

-4

I created an MVC project and within an API.

I need to get the API when I upload the project to (F5).

Example, I created a service that is a GET in my Type table.

How can I map the API to be able to get inside an MVC controller?

My project is Asp.Net MVC and inside it I have a Web API project, the services are in the Web API and by MVC I should get the service and fill in some Views.

This method works inside the API service, but now with MVC I do not know how to call.

public class GetCidades
    {
        BancoContext banco = new BancoContext();

        public List<Cidade> getCidades()
        {
            var result = banco.Database.SqlQuery<Cidade>("sp_cons_cidade").ToList();

            return result;
        }
    }

How do I get this service inside the MVC. I did this

public class GetCidades
    {
        HttpClient client = new HttpClient();
        List<Cidade> cidades = new List<Cidade>();

        public async Task<List<Cidade>> GetCidadesAsync()
        {
            string url = $"http://localhost:51381/api/";
            var response = await client.GetStringAsync(url);
            var _cidade = JsonConvert.DeserializeObject<List<Cidade>>(response);
            return _cidade;
        }
    }

But I do not know what else to do.

Do I need to map anything? Because when I give F5 up the two, right, MVC and API and from within the MVC how do I get the API?

EDIT1

My controller within the MVC

public class GetCidadeController : Controller
    {
        // GET: GetCidade
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetCidades()
        {

            return View();
        }
    }

The API controller

[RoutePrefix("api/[controller]")]
    public class GetCidadesController : ApiController
    {
        GetCidades cidades = new GetCidades();

        [AcceptVerbs("Get")]
        public IEnumerable<Cidade> getCidades()
        {
            return cidades.getCidades().AsEnumerable().ToList();
        }

    }

Lost. I do not know if this is how it should be done

    
asked by anonymous 08.08.2018 / 19:54

1 answer

1

Try to point the browser to / api / [name of your controller, without the controller suffix] / [name of your method or action]

This is the default mapping. If you can post your class Startup.cs and the controller here can be more precise.

It seems to me that you are not putting your service into a controller. In Web.Api design, you must have a class that inherits from Controller. In this class, a method called Get () where in this method you use your "GetCities" and it returns what it returns.

I think you're missing out on understanding how Asp.Net works, take a look at this link that might help you: #

    

08.08.2018 / 20:04