4

In our application, which is Single Page application, we are using MVC controller(Action methods as API) for CRUD operation's. Which I feel its wrong.

Can someone tell me if its correct?

Eg:-

I have an API Controller say :-

public class MockAPIController : ApiController
    {
        // GET api/MockAPI/5
        public ClassA GetSomething(int id)
        {

            return new ClassA();
        }
    }

and this can be called from client-side using /api/MockAPI/GetSomething/1. Similarly if I create MVC Controller like:-

public class MockAPIController : Controller
    {
        // GET api/MockAPI/5
        public ActionResult GetSomething(int id)
        {

            return new JsonResult(new ClassA(),JsonRequestBehavior.AllowGet);
        }
    }

Still, I can get it work. Can some-one tell me whats demerit of using MVC controller for API?

EDIT:-

Is it recommended to use MVC Controller for API methods?? If Not, then can someone point out the -ve aspect of it?

4 Answers 4

1

Using the web API you can return objects as normal and your clients can specify the content-type.

This will automatically serialize the objects to xml or json without the need to specify a new action just to change the return type.

So your API call will always remain as:

public ClassA GetSomething(int id)
{

    return new ClassA();
}

But it is capable of returning xml and json without any changes in the controller.

Sign up to request clarification or add additional context in comments.

2 Comments

Returning objects like this is one of the worst ways to use Web API. I think the team partially realized this and have since introduced IHttpActionResult as an alternative. IMHO, returning HttpResponseMessage is by far the best way in Web API.
@darrelmiller yes, I totally agree. I was trying to get the answer in quickly & forgot to set the return type response code. However I still think it's valid to use web api in the manner I mention. Before it I would mess about with wcf to achieve the same thing (multi-client capable restful api) which took longer.
1

Web API provides cleaner way to craft your HTTP responses. It is extremely extensible, testable and faithful to the HTTP spec.

Web API was NEVER intended to provide an "out-of-the-box" REST framework.

MVC is a HTTP framework, optimized for serving content to a Web Browser. Web API has no bias as to what client is using it.

Comments

0

As per my understanding,

MVC controllers are extended over API Controller. You can do almost everything what can be done with API Controller. (People please correct me if I am wrong, its purely my understanding which I am sharing!)

Now, if your application is a web based application/internet or intranet where you have very few api call's then you can stick with MVC Controllers. Only care that need to be taken care is sending data as JsonResult(basically serializing`to JSON or whatever). If you have more funda of SPA, the API controllers is what you need.

I myself have not found much articles stating a strong line separating what to use when, it's always a developers pain to decide and implement.

Comments

0

It's less about merit and demerit of using ApiControllers in place of Controllers, and more about implementation and usage.

ApiControllers are specifically meant for building REST-ful Apis which return serialized data to the client in simplest form.

And, using controllers you can return Views, Different Types of ActionResults in different form. You can definitely convert the type of data you are returning, like the way you are using here but it's not the same with ApiControllers.

2 Comments

I can assure you that ApiControllers were not designed for specifically building RESTful APIs. However, they don't prevent you from doing it. Returning serialized domain objects is a quick recipe for violating a number of REST constraints.
Thanks Darrel for catching me on that line. I hope then I have to dig deeper into that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.