Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an MVC Controller Proxy for a Web API Controller

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!

like image 973
jkruer01 Avatar asked Dec 29 '14 13:12

jkruer01


People also ask

How do I create a proxy for Web API?

The easiest way to create an API proxy is using the Create Proxy wizard. To access the Create Proxy wizard using the Edge UI: Sign in to apigee.com/edge. Select Develop > API Proxies in the left navigation bar.

How do you post to a Web API controller from an MVC controller?

In order to add a Web API Controller you will need to Right Click the Controllers folder in the Solution Explorer and click on Add and then Controller. Now from the Add Scaffold window, choose the Web API 2 Controller – Empty option as shown below. Then give it a suitable name and click OK.

Can I add API controller to MVC project?

If you have MVC project and you need to add Web API controller to this project, it can be done very easy. 1. Add Nuget package Microsoft. AspNet.

Can a Web API controller return an MVC view?

You can return one or the other, not both. Frankly, a WebAPI controller returns nothing but data, never a view page. A MVC controller returns view pages. Yes, your MVC code can be a consumer of a WebAPI, but not the other way around.


1 Answers

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);
        }
    }
}
like image 163
aleha Avatar answered Oct 02 '22 23:10

aleha