Need ASP.NET WebApi from MVC

I started work on a new project in which I look at client functions through WebApi so that I can reuse the same functionality on multiple devices, but there are some functions that are shared between mobile devices and the hosting application.

My question is that I know how to consume WebApi from devices. I am writing a little empty for the best way, best practice or just something that works on how to use WebApi from an MVC project that has an api hosting.

My first thought was to simply instantiate the controllers as needed, as if I were some other class, however I have a hidden suspicion that although it will work, this is a bad approach.

Another thought would be to use HttpClient. Although I was lucky with him since I can never find api

var client = new HttpClient(new HttpServer(GlobalConfiguration.Configuration)); client.PutAsJsonAsync("/api/project/login", Json(model)); 
+7
c # asp.net-mvc asp.net-web-api asp.net-mvc-4
source share
1 answer

This is a slightly subjective question, as there really is no β€œright” answer. In fact, you need to consider a few points.

  • How safe is it to follow a simple route, and does the dependency allow the controller instance?
  • Will this logic be used elsewhere?
  • Are these two issues separate applications?

In essence, if the API and the MVC application have a difference problem, then they should be properly separated, and the MVC application should call the WebAPI application using the HttpClient. This can significantly reduce attacks and individual applications.

If MVC and WebAPI display the same information at the same time, and security / separation of problems is not a problem, then it is enough to enable controller dependency management.

If logic does not share the problem of problems, and it will be used in several places, then factoring it into common components is likely to be your best bet.

Shooting from the hip sounds like a small application that can benefit from dependencies that allow the controller in the near future. As the application grows (or if it is already planned), splitting the logic into the correct SOA format will be useful.

tl; dr - SO really cannot answer this question without knowing what such an application will do. Be pragmatic.

+2
source share

All Articles