Map Api port to MVC project

Asked

Viewed 82 times

-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

  • 1

    Making a request or post according to the verb accepted by the method?

  • @Leandroangelo, friend, how do I do it? I don’t know how to do it.

  • 2

    @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.

  • 1

    Just downvote this week, it’s week or month of downvotes

  • @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)

  • 1

    @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 put more snippets of code, more things you have tested. If you put this, it becomes easier for us to help.

  • Blz, we have a little code, now focus on describing the error

  • They are two different projects within the same Solution and you have configured to uppar the two?

  • Delete comments to avoid further polluting the question, and eventually it will be easier to help in solving the question

  • 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, 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?

  • Yeah, @Marceloboni, that’s right

Show 8 more comments

1 answer

1


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

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

It seems to me that you are not putting your service inside a controller. In the Web.Api project, you must have a class that you inherit from Controller. In this class, a method called Get() where in this method you use your "Getcities" and return what it returns.

I think it’s missing understand how Asp.Net works, take a look at this link that can help you: https://docs.microsoft.com/pt-br/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

  • I don’t have Startup.cs. My project is Asp.Net MVC and within it a Web API project, the services are in the Web API and by MVC I must take the service and fill in some Views.

  • @pnet for conversation starters: this was information that should already be in the question. By not doing this, you induce the author of the answer to fail.

  • andrecarlucci, if I do it this way I can’t get the API. Do I need to map something? Because when I give F5 up the two, right, MVC and API and from inside the MVC as I get the API?

  • Andre, I did the project first in web api and managed to get the services, but after I had to create a MVC and with it consume the Web API, then I could not more.

  • If you have the services, you could post the controller to us?

  • Solved by the answer of Andre. It is giving another error here but this is another POST

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.