L-Four
L-Four

Reputation: 13531

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

Upvotes: 6

Views: 1011

Answers (1)

moribvndvs
moribvndvs

Reputation: 42497

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.

Upvotes: 3

Related Questions