2

I need to build a massive API and I'm trying out WebAPI instead of default MVC4 projects and it seems that it just makes things more difficult.

  • Can have only 4 methods in controller Get, Post, Put, Delete, if I want more I need to modify route for that particular method
  • FluentValidation won't work with WebAPI so I need to use DataAnnotations which I really don't want to.
  • Can't use dynamic return data-types

My question is: Would it really be that bad if I would use MVC4 project and use default ActionResults that return Json? What are real advantages of using WebAPI, why did they even made them in the first place if you can easily convert your project to API?

Stan
  • 25,744
  • 53
  • 164
  • 242
  • 4
    We managed to integrate FluentValidation in WebApi. All we had to do was implement a custom ModelValidatorProvider. See this SO for more detail: http://stackoverflow.com/questions/12975291/how-to-hook-fluentvalidator-to-a-mvc-4-web-api. For "dynamic" data type, I can't see why you would like to return a `dynamic` to the client.. Did you mean anonymous object ? – Simon Belanger Jun 29 '13 at 22:33
  • If you only care about JSON, then you can return a dynamic object from a Web API controller. (The XML serializer doesn't support dynamic.) For routing, you can use action-based routing (put "{action}" in the route template, or use attribute-based routing. – Mike Wasson Jul 01 '13 at 22:23

3 Answers3

2

You should most definitely give NancyFx a try (ASP.NET Web API like REST framework but lightweight). Then you can have methods like:

public class SampleModule : Nancy.NancyModule
{
    public SampleModule()
    {
        Get["/"] = _ => "Hello World!";
    }
}
Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
1

You can but you should not use JsonResult. Web Api is a genius concept. Web APi has made REST simpler. For every entity you can create new ApiController and manage CRUD operations through it. Here are some points.

  1. You have mentioned that we can't use actions more than Get, Post, Put, Delete which is not true. I have used following actions.

    public bool post()
    {
       return true;
    }
    
    [HttpPost]
    public bool validate(int id)
    {
       return input == 0;
    }
    

So in the above code, if you do post operation with /cntrName, post method will be called and if you post /cntrname/validate/4, validate will be called. It means you can use function overloading for mutliple requests.

2. I use angularJS a lot and Web Api helps tremendously. You have to declare 1 url per Angular controller and every request looks same but makes appropriate calls. 3. It increases reusability.Example

[HttpPost]
public JsonResult post()
{
    return Json("output",JsonRequestBehavior.AllowGet);
}

public string post()
{
    return "output";
}

Now if you want to use above method in different controller, with MVC you have to do following

(string) post().Data //get returned data from JsonResult and convert to string.
Web Api is just returning string
  1. Web API is a logical abstract for doing ajax requests. Sure you can use MVC (You can use WebForms also if you want). But web api is better way of writting CRUD-REST applications.
om471987
  • 5,398
  • 5
  • 32
  • 40
0

If you are happy with IIS as a host and you only want to return JSON and HTML and you are already familiar with ASP.NET MVC then I would suggest you stick with it.

WebAPI only really starts to show its advantages when you want more flexibility with media types, more host options and more direct control over HTTP headers.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243