简体   繁体   中英

Passing objects to a Web API from an MVC application

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM