1

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

asked Jun 2, 2017 at 13:53
21

1 Answer 1

1

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"});
}
answered Jun 3, 2017 at 0:13
2
  • 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 Commented Jun 3, 2017 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. Commented Jun 3, 2017 at 8:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.