What is the best way to create a simple REST API in Visual Studio 2013 using ASP.NET MVC.
I just want something with controllers and route config along with any other ba
I suppose the best way for your project is to use WebApi even with simple HTML page, but... jQuery and even something like Knockout.JS or Angular, this is MUST HAVE for any modern project.
You can remove the MVC component if you wish, and just use Web API 2 stuff. Add your own custom config bit:
//attribute routing - lets me put any route any method in any darn file.
//this sets you free. I could never see doing it any other way.
config.MapHttpAttributeRoutes();
So that you can create any route you wish on the fly, in any file you create as a Controller.
public class MyWeirdController : ApiController {
//via REST, get object //method-route defined here as I want it
[HttpGet] [Route("~/my/custom/route/someobject/{objectid}")]
public CustomObject GonnaGetCustomObject(int objectid) {
.... use whatever
var obj = new CustomObject();
obj.SetSomething = true;
return obj
}
}
You don't need MVC. I use WebAPI2 with Linq2SQL (Entities is more of a PITA when I want simple) - but that part isn't needed either. L2SQL entities are just faster/easier to stand up and get running with their modeling classes - but any class or whatever you want, send it.
I usually use Knockout if I want to MVVM it on the javascript side. With KO.Mapping -- I can, with one line of code - receive that CustomOBject
and have a JS object to work with and instantly bind to elements on the page. The toughest part of all of this is learning Knockout. Custom attribute writing with WebAPI2 is crazy easy and fun.
In short, WebAPI2 with custom attribute routing is all you need to stand up a REST client in any way you want. Break out your "controllers" any way you wish with any methods/returns/types as you want. You can make it as simple as you want, or start tossing in things like Knockout and L2SQL (or EF) to type-bind things even closer.
It's all in the template you select.
Now you'll have the bare minimums created.
See: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
Depending on what parts of ASP.NET you want, you could check out Nancy at https://github.com/thecodejunkie/Nancy. It can be hosted within ASP.NET and is pretty light-weight and provides a simple routing with options to plug in different view technologies (Razor, Spark, SimpleView and more).