Monday 24 February 2014

Windows Azure Mobile Services - Web API - Custom APIs

So what about the API tab that is in the Node.js service and missing from the Web API service? Well, the clue is in the name! We can simply add a Web API 2 controller to our solution like this:


And start coding in our controller methods. The 'RequiresAuthorization' security attributes we looked at in this article still work and we can create a reference to the 'ApiServices' object to access logging, push client etc:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.WindowsAzure.Mobile.Service;
using Microsoft.WindowsAzure.Mobile.Service.Security;

namespace TileTapperWebAPIService.Controllers
{
    public class HighScoreController : ApiController
    {
        // GET api/<controller>
        [RequiresAuthorization(AuthorizationLevel.Application)]
        public IEnumerable<string> Get()
        {
            var service = new ApiServices(base.Configuration);
            service.Log.Info("Hello from HighScoreController!");

            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }

}

No comments:

Post a Comment