Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke MVC controller from a class

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.

like image 355
user2905455 Avatar asked Aug 06 '26 13:08

user2905455


1 Answers

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.

like image 197
Xavier Egea Avatar answered Aug 09 '26 02:08

Xavier Egea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!