6

I have an MVC project that is exposed externally. I have an internal Web API project.

For reasons beyond my control, I cannot expose the Web API project directly and I cannot add Web API Controllers to my MVC project.

I need to create an MVC Controller that will act as a proxy for a Web API Controller. I need the response from the MVC Controller to look as if the Web API was called directly.

What is the best way to accomplish this?

Is there a better approach than what I have so far?

How can I fix the error that I am getting?

Here is what I have so far:

MyMVCController

[HttpGet]
public HttpResponseMessage GetData(HttpRequestMessage request)
 {
 ...
 var response = proxy.GetData();
 return request.CreateResponse();
 }

MyProxyClass

public HttpResponseMessage GetData()
 {
 ...
 return HttpRequest(new HttpRequestMessage(HttpMethod.Get, uri));
 }
private HttpResponseMessage HttpRequest(HttpRequestMessage message)
 {
 HttpResponseMessage response;
 ...
 using (var client = new HttpClient())
 {
 client.Timeout = TimeSpan.FromSeconds(120);
 response = client.SendAsync(message).Result;
 }
 return response;
 }

In the MVC Controller, I am getting an InvalidOperationException on the request.CreateResponse() line. The error says:

The request does not have an associated configuration object or the provided configuration was null.

Any help would be greatly appreciated. I have searched Google and StackOverflow but I haven't been able to find a good solution for creating this proxy between MVC and Web API.

Thanks!

asked Dec 29, 2014 at 13:51

1 Answer 1

6

You can do it by just creating some JsonResult action in your controller which will return result of calling web API.

public class HomeController : Controller
{
 public async Task<JsonResult> CallToWebApi()
 {
 return this.Content(
 await new WebApiCaller().GetObjectsAsync(),
 "application/json"
 );
 }
}
public class WebApiCaller
{
 readonly string uri = "your url";
 public async Task<string> GetObjectsAsync()
 {
 using (HttpClient httpClient = new HttpClient())
 {
 return await httpClient.GetStringAsync(uri);
 }
 }
}
Quality Catalyst
6,8318 gold badges41 silver badges65 bronze badges
answered Dec 29, 2014 at 14:02
5
  • 1
    Wouldn't that always return a 200 status code? I need to preserve whatever status code of the response from the Web API. Also, I don't want the Web API to serialize the object, the proxy de-serialize it, and then the MVC serialize the object again. Commented Dec 29, 2014 at 14:10
  • you could always return status code you want base on result you achieve from web api call Commented Dec 29, 2014 at 14:12
  • what about the serialize, de-serialize, serialize again issue? Commented Dec 29, 2014 at 14:22
  • Also, what would happen if the Web Api returned an error message/response. The WebApiCaller would try to deserialize it into the expected object type which would then throw an error right? Commented Dec 29, 2014 at 14:30
  • modified. Withou deserialization. For details look here: stackoverflow.com/a/9777889/1235390 Commented Dec 29, 2014 at 14:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.