We're trying to pass objects from an MVC application to a webapi application. Basically our set up is :-
Models project MVC 4 web project - references the models project WEB API project - references the models project
from the mvc application, we want to pass a populated model to the web api,
User Model
public User
{
Firstname { get; set}
Surname { get; set}
}
Web API
public bool Add(mUser use)
{
}
MVC
http headers? URL string? should we be using http client? also, are there any samples? I've only found 1 but unable to get this to work.
any help or advise would be greatly appreciated.
-
2Why? Why would you want to handle an HTTP request, by issuing another HTTP request to a service that you control? Why not just allow your MVC site to access your businesss layer directly?Darrel Miller– Darrel Miller2013年11月24日 01:05:40 +00:00Commented Nov 24, 2013 at 1:05
-
This is because the web API will be released separately, and we will require multiple application to communicate with this service. This service will act as a gatewaySimon– Simon2013年11月24日 09:28:01 +00:00Commented Nov 24, 2013 at 9:28
-
It is not uncommon to have a web api in between in case where the service needs to talk to several applications, it is NOT using an http request to access another one, it's an extra layer which allows your application to be more robustJose– Jose2013年11月24日 20:16:57 +00:00Commented Nov 24, 2013 at 20:16
1 Answer 1
Basically there are 2 approaches:
- The MVC application performs a full HTTP request to the Web API
using an HTTP client
. This allows you to have your Web API hosted in a separate process than the MVC application and scale it independently. The Web API could be hosted on a separate web farm than the MVC application and both will have completely different lifetimes/ - The Web API and MVC application are hosted in the same ASP.NET process and the MVC application consumes the Web API with direct calls using an
HttpMessageInvoker
as shown inthis post
. The advantage here is that the client is not paying the overhead of an HTTP call being made. The drawback is that the 2 applications are tightly coupled and relying on the fact that are hosted in the same process.
The 2 approaches are valid, it's up to you to decide which one fits better your needs.
Explore related questions
See similar questions with these tags.