0

Here is my Controller -

    namespace MvcApplication.Controllers
    {
      public class HomeController : Controller
      {
         public ActionResult Contact()
         {
           ViewBag.Message = "Your contact page.";
           return View();
         }
         public JsonResult GetPartNumbers()
         {
           var PartNumbers = ProductModel.LoadAllPartNumbers().ToArray();
           return Json(PartNumbers, JsonRequestBehavior.AllowGet);
         }
     }
   }

I haven't inherited from ApiController. Still it's working as it can return ActionResult data types for MVC requests as well as basic data types for api requests. Here is my WebApiConfig file -

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

I am using MVC4. Now the main question is how come this working as i have not inherited from ApiController, and then what is the need of ApiController if this can work?

EDIT

public bool SaveEvent(string PartNumber, string DateTimeScheduled, string DateTimeEnd, string Notes, string PSI)
    {
        return DiaryEvent.CreateNewEvent(PartNumber, DateTimeScheduled, DateTimeEnd, Notes, PSI);
    }

This above method does not return an ActionResult, but a primitive type but this is called via ajax and can return bool true or false

Krishna
  • 5,194
  • 2
  • 28
  • 33
  • How you sure that it works for same controller ? How you access it ? – dotnetstep Dec 22 '14 at 07:51
  • I'm not sure I understand the question - MVC has `ActionResult` and `JsonResult`. Is `/api/home` working for you? – Kobi Dec 22 '14 at 07:52
  • 1
    You are working on the rest style of MVC. Rest can return results only.http://stackoverflow.com/questions/9494966/difference-between-apicontroller-and-controller-in-asp-net-mvc – Saravanan Dec 22 '14 at 08:00
  • However in next versions, we have a single controller that has the abstractions for MVC & web api. In that case, you won't be able to differentiate between either. – Saravanan Dec 22 '14 at 08:03
  • @dotnetstep yes.. i debug the application and i can see it's hitting the same controller, for both the methods(actions) – Krishna Dec 22 '14 at 08:05
  • @Krishna Are you sure that it is using Web API route because your application has MVC route and that is used to call that method not web api. Also you have edited question and that method can also be work as MVC route. – dotnetstep Dec 22 '14 at 08:07
  • @Kobi I have made an an edit to the question, now i think you can understand what i want to ask – Krishna Dec 22 '14 at 08:09
  • @dotnetstep i dont know which route it's using but how can an MVC controller return bool value? – Krishna Dec 22 '14 at 08:11
  • @dotnetstep so we can call a method(action) which does not return ActionResult via MVC route? – Krishna Dec 22 '14 at 08:14
  • 1
    Yes. Unless it is not mark as NoAction – dotnetstep Dec 22 '14 at 08:17

1 Answers1

2

You HomeController as Method that return Json

if your web url like this http://yourdomain/home/GetPartNumbers it return Json result.

Now as per your edit you have method called SaveEvent. Which is called as http://yourdomain/home/SaveEvent. This also called by MVC pipeline. When you return result like true or false or any string which is not type of ActionResult , it get converted ContentResult and return to your AJAX call.

Here Web API route will not get called.

Even when WEB API is not available at that time Developer use MVC as REST as well.

To check further put following code in your controller and you can see which result type your method get converted when you not return actionresult.

protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            string resultType = filterContext.Result.GetType().Name;
            base.OnActionExecuted(filterContext);
        }

If you don't want such action get called then you have explicitly set NonAction attribute for them or make then private.

[NonAction]
        public object Test()
        {
            return new { test = "Jinal" };
        }
dotnetstep
  • 17,065
  • 5
  • 54
  • 72
  • Thanks!! I knew that an Action must return ActionResult type otherwise that action will not work. So basically an action can return basic types and those types are converted to ContentResult right!! – Krishna Dec 22 '14 at 08:19
  • Yes. Even you can return object of class. In this case it will call tostring method and return as a result. – dotnetstep Dec 22 '14 at 15:08