0

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.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/c81ee004-67f6-4f9e-8b72-1e46378b39c0/how-to-pass-several-objects-to-web-api-method?forum=csharpgeneral

any help or advise would be greatly appreciated.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Simon
  • 1,412
  • 4
  • 20
  • 48
  • 2
    Why? 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 Nov 24 '13 at 01: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 gateway – Simon Nov 24 '13 at 09: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 robust – Jose Nov 24 '13 at 20:16

1 Answers1

1

Basically there are 2 approaches:

  1. 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/
  2. 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 in this 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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928