0

I have an action method which is called by javascript on the front-end using $.getJSON(path) which looks something like below in order to populate a dropdownlist:

 [HttpGet]
        public List<SelectListItem> GetSomeList(int cId)
        {...}

I now need to modify it so that I can return some custom logic based errors based on certain conditions. What's the best way to go here? Should I follow this and go with IActionResult? Please note my question is more specific to Core MVC and not Web API.

Riz
  • 5,680
  • 16
  • 56
  • 91
  • 4
    To return exception/error messages as JSON, the answer is yes, you need to change return type to `IActionResult` and put `return new JsonResult(...)` containing error message you want to show on successful AJAX callback. – Tetsuya Yamamoto Jan 02 '19 at 06:20
  • thank you. if you respond with an answer, i'll mark it as such. – Riz Jan 02 '19 at 06:21
  • 1
    I found a similar issue: https://stackoverflow.com/questions/39353016/asp-net-core-error-json. You can adjust the error message and status code of the response based from provided solutions there. – Tetsuya Yamamoto Jan 02 '19 at 06:59

0 Answers0