3

I have been asked to build an MVC 4 project where the controller method can be invoked by an external service.

I have very limited information regarding this external service except that each service endpoint will render a different view.

My job is to create these views and have the controller be invoked manually to render the view the service endpoint asks for.

The service and the MVC project will be built as separate projects. The MVC project is a standalone project. The service expects the HTML of the form back, which it would pass on to another project.

I don't know a way to invoke controller through code-behind from a project that lives in a different solution. Any link/tutorial would be great.

Apologies, if my question seems vague or lack information. Please let me know if it does, and I'd add more information.

  • Just to understand...if the external service can call your intermediate layer, why it's not calling directly your MVC controller actions doing Posts? – Xavier Egea Jun 30 '15 at 07:10
  • @Freerider it definitely can - I just thought it'd be better to add an intermediate layer. Regardless, I just don't know how to invoke controller method from the service either. – user2905455 Jun 30 '15 at 07:11
  • I didn't understand your question? you want your views directly call services or your actions call services – Ali pishkari Jun 30 '15 at 07:54
  • Didn't understand - "The MVC project is a standalone project and won't be hosted". You will not be able to make any use of MVC project until it is hosted. – Yogi Jul 01 '15 at 04:06
  • Please specify, in what format the response is required at external service end. In Json, Xml or Html format? And how the external service will use this data, because then only you can take the decision on how to design your solution – Yogi Jul 01 '15 at 04:17

2 Answers2

1

I suggest you to avoid the intermediate layer and call directly your MVC controllers or Web Api directly from the external service.

A quick example querying an external resource:

public string ListProductsAsync()
{
    var client = new HttpClient();

    var response = client.GetStringAsync("http://youriste/controller/action");

    return response.Result;
}

For more advanced explanation, the following link (Calling a Web API From a .NET Client in ASP.NET Web API 2) will help you.

Xavier Egea
  • 4,712
  • 3
  • 25
  • 39
  • my confusion is regarding the controller methods invocation. How to do that from code-behind? – user2905455 Jul 01 '15 at 00:52
  • @user2905455 The way you should call a controller is by performing GET, POST, PUT, ... operations. In the same way you'd call from a user interface to your controllers, there exists the option to call controllers from server to server, using the http mehods GET, POST, ...as my answer shows, using for example GetStringAsync() method from HttpClient. If you call your action controllers in other way, it has no sense to have controllers. – Xavier Egea Jul 01 '15 at 07:41
0

You may consider creating API Controllers (WebAPI) instead of normal Controller.

And then return view as shown here - https://aspguy.wordpress.com/2013/09/10/web-api-and-returning-a-razor-view/

The WebAPIs are exposed as services to clients and they can consume them like just another service.

However this would be a hack, and it is suggested to use WebAPIs as restful services and returning xml or json only.

Now the response is expected in HTML, You can use HttpClient to call your controller (MVC or WebApi) from the calling application.

Few links -

http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

async calls using HttpClient on MVC4

Community
  • 1
  • 1
Yogi
  • 9,174
  • 2
  • 46
  • 61
  • I'll have a look at that link, thanks. Quick question - would an external service be able to invoke APIController's methods? If so, is there an example that I can have a look at? – user2905455 Jun 30 '15 at 07:24
  • @user2905455: Yes it can, a quick start tutorial on WebApi would certainly help. See - http://www.asp.net/web-api – Yogi Jun 30 '15 at 07:26