Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consuming ASP.NET MVC action methods

current situation: an ASP.NET MVC web site with a number of controllers and action methods, and views to allow adding recipes.

Now, I have to create a WPF application that acts as a UI to add recipes (same as the web site).

My question is: can I use ASP.NET MVC site to expose service operations that are consumed by the WPF application (and how can this be done)? Or should I better create dedicated WCF services for that, and have the WPF AND the ASP.NET MVC site consume these services?

Thanks, Ludwig

like image 482
L-Four Avatar asked May 07 '12 08:05

L-Four


People also ask

What are action methods in ASP.NET MVC?

ASP.NET MVC Action Methods are responsible to execute requests and generate responses to it. By default, it generates a response in the form of ActionResult. Actions typically have a one-to-one mapping with user interactions.

How many types of action methods are there in MVC?

There are two methods in Action Result. One is ActionResult() and another one is ExecuteResult().

How can call action method from view in ASP.NET MVC?

You can specify an action via the MVC namespace. When given a Controller, such as HomeController : public class HomeController : Controller { public ActionResult Index() { ... } public ActionResult MyAction() { ... } public ActionResult MyActionWithParameter(int parameter) { ... } }


1 Answers

I have successfully used MVC controllers and actions to service both HTML views from the browser, as well as external applications. It works well, but as it stands you'd need a little bit of tooling:

  1. I have an action filter that returns the Model resulting from an action in a format the client requested (by inspecting the Accepts header, looking for either application/json or text/xml). So, I can serialize the resulting model as JSON or XML (I prefer JSON).
  2. You will need to find or create a simple API in your client application to create WebRequests to your actions, and then process the results. I created a simple API that can POST or GET, and then deserializes any resulting JSON into an object (using JSON.NET). There are REST client APIs out there you could use for this.

However, you could avoid some of this extra tooling if you go the WCF-REST route. Better yet, I'd look into ASP.NET MVC 4's WebApi feature (which is what I will be migrating to).

For the record, I think WCF is powerful, but our organization has grown tired of how complicated it can be to turn all the knobs and hit all the switches to get it working right, and set up easily from one install to the other. MVC, on the other hand, just works... and since we already use it to service our HTML views, it's a real joy to only have to add a little extra code to have it handle service calls, too. Just a personal preference, of course.

like image 179
moribvndvs Avatar answered Oct 02 '22 17:10

moribvndvs