-4
I created an MVC project and 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 I can map the API to get inside a MVC controller?
My project is Asp.Net MVC and within it I have a Web API project, the services are in the Web API and by MVC I must take the service and fill in some Views.
This method works within the API service, but now with MVC 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 I take this service within 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 don’t know what else to do.
Do I need to map something? Because when I give F5 up the two, right, MVC and API and from inside the MVC how do I get the API?
EDIT1
My controller inside the MVC
public class GetCidadeController : Controller
{
// GET: GetCidade
public ActionResult Index()
{
return View();
}
public ActionResult GetCidades()
{
return View();
}
}
The controller of the API
[RoutePrefix("api/[controller]")]
public class GetCidadesController : ApiController
{
GetCidades cidades = new GetCidades();
[AcceptVerbs("Get")]
public IEnumerable<Cidade> getCidades()
{
return cidades.getCidades().AsEnumerable().ToList();
}
}
Lost. I don’t even know if that’s the way to do it
Making a request or post according to the verb accepted by the method?
– Leandro Angelo
@Leandroangelo, friend, how do I do it? I don’t know how to do it.
– pnet
@pnet it’s time to take a reevaluation in the way you ask things here on the site. You have received negative one after the other.
– Wallace Maxters
Just downvote this week, it’s week or month of downvotes
– pnet
@pnet but with all respect, the questions are even poorly formulated. It will hardly help other people the way it is designed (if it is possible to answer clearly even for your problem)
– Wallace Maxters
@Wallacemaxters, I fully agree with you. But the doubt is great and sometimes I don’t know what to do. I will try to improve the questions. It’s really wide open
– pnet
@pnet put more snippets of code, more things you have tested. If you put this, it becomes easier for us to help.
– Wallace Maxters
Blz, we have a little code, now focus on describing the error
– Leandro Angelo
They are two different projects within the same Solution and you have configured to uppar the two?
– Leandro Angelo
Delete comments to avoid further polluting the question, and eventually it will be easier to help in solving the question
– MarceloBoni
I went to Solution properties and marked that the project depends on the API. Now I think it goes up both, right or not?
– pnet
@pnet, I do not know the world of c#, but I think you can make a comparison, in Aravel you have an API environment and a WEB environment, you have this in your project and do not know how to communicate with each other.... that’s it?
– MarceloBoni
Yeah, @Marceloboni, that’s right
– pnet