I have been trying to build an MVC structured RESTapi.Since it's going to return JSON only,is it right that my View will just have a header and that the view will output the JSON also?

I planed to implement JsonSerializable than to do some magic with it in the controller/model part, than to output it in the view.

Is it the right way?

I feel that my view is small if i would do it this way,but i red lots of articles about it and i think that it's right.Since it't returning only JSON and there is no html/css/js.

Since i planed to move this part to another domain for example:api.domain.com

Than i would make the visual part in Angular probably and it would use the api.domain.com


A View is something you look at. Unless there is something very unusual about your user base, most of them aren't going to want to look at JSON.

If you need to return JSON from an MVC controller action, the traditional way to spit that data out is to use a JSONresult and not a ViewResult. The JSON data would typically be output in the body, not in the headers, using a content-type of application/json. If you are using .NET, code might look like this:

public ActionResult SomeActionMethod()
    return Json(new {foo="fooValue", bar="barValue"});
  • I wrote 'View will just have a header and that it will output the JSON' you might be miss undarstood this part.No I built it in PHP.Here is the project github.com/rarslan/PHP-MVC-RESTful-API – DaAmidza Jun 3 '17 at 8:26
  • My question was if it is okay.Since the controller/model will send the serialized Json to the view and it will have just a header and encode it. – DaAmidza Jun 3 '17 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.