ASP.NET MVC: It would be good practice for the API controller action to return to both View and / or JSON

I am programming an API to be used by both a web application and a mobile application, and I am using ASP.NET MVC 2 in my technology stack.

I currently have a Rest-like API service that returns data in JSON format. This works well for a mobile application, but I also want it to work for a web application as well.

Will the controller effect return either HTML View or JsonResult for this good approach?

The only difference between the web application and the mobile application is the presentation level; application logic is the same.

I think I could create a controller that is used for the web application, but I think that a lot of logic would be duplicated from the API controller.

Edit

I have another layer that handles all the application logic, but the API controller still has some logic for checking parameters and handling errors when it returns a JSON response. So far, duplicate logic has been part of the test.

Here are some code snippets:

public JsonResult GetList(string accessToken, string listId) { if (string.IsNullOrEmpty(accessToken)) return Json(new { success = false, exceptionMessage = "Facebook access token is required." }); if (string.IsNullOrEmpty(listId)) return Json(new { success = false, exceptionMessage = "The list id is required." }); string facebookId = null; var facebookIdParseSuccess = GetFacebookId(accessToken, out facebookId); if (!facebookIdParseSuccess) return Json(new { success = false, exceptionMessage = "There was a problem accessing your Facebook profile information." }); try { _groceryListManager.FacebookId = facebookId; var groceryList = _groceryListManager.GetList(listId); GroceryListViewModel mappedList = new GroceryListViewModel(); Mapper.Map(groceryList, mappedList); return Json(new { success = true, results = mappedList }); } catch (Exception ex) { return Json(new { success = false, exceptionMessage = "..."}); } } 
+4
source share
4 answers

I saw how one action method came back earlier, but, in my opinion, you better have two separate action methods. You can force them to bypass another method with common code, but since they are used for two very different things, you may find that they are easier to support if you have two methods (future requirements may lead to a change, which makes it difficult to support both method).

Whether they are in the same controller or not, they really depend more on the nature, size and complexity of the application. I separated the API as a completely different project from HTML. I shared a dll to access data and other common functions, but MVC projects are different.

+1
source

If your view model is suitable for returning as JSON, then performing both actions in the same action may work well. Sort of:

 public ActionResult Foo() { FooModel model = new FooModel(); // Code here to build the model if (Request.IsAjaxRequest()) return Json(model); else return View(model); } 

It will also help you succeed in using progressive improvement. If the URLs for HTML and JSON are the same, it’s much easier to use the available URLs for search and optimization, and then gradually improve the addition of unobtrusive event handlers to replace them with JSON requests in JavaScript-enabled browsers.

+2
source

Depending on what you definitely want to achieve:

+1
source

but I think that big logic is duplicated using an API controller.

I think you yourself answered the question. I would keep them in one controller. :)

0
source

Source: https://habr.com/ru/post/1334129/


All Articles