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
-
Could you share any of the links (articles) where supposedly they implement this approach?Laiv– Laiv2017年06月02日 20:58:03 +00:00Commented Jun 2, 2017 at 20:58
-
codereview.stackexchange.com/questions/164468/…DaAmidza– DaAmidza2017年06月03日 08:53:28 +00:00Commented Jun 3, 2017 at 8:53
-
Looking at other MVCs (viralpatel.net/blogs/spring-4-mvc-rest-example-json)DaAmidza– DaAmidza2017年06月03日 08:54:22 +00:00Commented Jun 3, 2017 at 8:54
-
stackoverflow.com/questions/9275613/is-mvc-restful-by-designDaAmidza– DaAmidza2017年06月03日 08:54:49 +00:00Commented Jun 3, 2017 at 8:54
-
I could go like this till tomorrow @Laiv the point is that i wanted someone to explain me in detail with references to tell me it's the right way.Tho i could't think of another when reading all this stuffDaAmidza– DaAmidza2017年06月03日 08:55:57 +00:00Commented Jun 3, 2017 at 8:55
1 Answer 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"});
}
-
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-APIDaAmidza– DaAmidza2017年06月03日 08:26:22 +00:00Commented 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.DaAmidza– DaAmidza2017年06月03日 08:30:05 +00:00Commented Jun 3, 2017 at 8:30