I added the AspNet.WebAPI to my existing MVC project with help from this post:
How to add Web API to an existing ASP.NET MVC (5) Web Application project?
It basically adds the following controller:
public class APIController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
...
And this in the App_Start:
namespace WebApplication1
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// TODO: Add any additional configuration code.
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// WebAPI when dealing with JSON & JavaScript!
// Setup json serialization to serialize classes to camel (std. Json format)
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver =
new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
}
}
}
This works, but for just for mysite/api/API How do I expand this to work with the rest of my existing controllers (for example mysite/api/CLIENTs)?
From looking at the code, it seems it should just work. I even tried putting "Get" functions in the other controllers but no luck. Actually I don't know why /API works--is it reading the file name of the controller?
So basically, I am looking for advice on where to go next--do I put code in each individual controller, or put many calls in my existing "APIController", or neither.