Azure Custom Controller / API .Net backend
Yes !!!
So I finally got it working, I copied the usings from lidydonna - msft git and read about .net backend for mobileservice.
This ended with the following:
using System.Web.Http;using Microsoft.Azure.Mobile.Server.Config;using System.Threading.Tasks;using System.Web.Http.Tracing;using Microsoft.Azure.Mobile.Server;namespace BCMobileAppService.Controllers{[MobileAppController]public class TestController : ApiController{ // GET api/Test [HttpGet, Route("api/Test/completeAll")] public string Get() { MobileAppSettingsDictionary settings = this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); ITraceWriter traceWriter = this.Configuration.Services.GetTraceWriter(); string host = settings.HostName ?? "localhost"; string greeting = "Hello from " + host; traceWriter.Info(greeting); return greeting; } // POST api/values [HttpPost, Route("api/Test/completeAll")] public string Post(string hej) { string retVal = "Hello World!" + hej; return retVal; } }}
This is a new controller and not the one that comes with it as lidydonna used. It seemed like it wants both functions get
and post
. This resulted in the API was registered and could be accessed. This means the client call to the server I used was:
t = await App.MobileService.InvokeApiAsync<string, string>("Test/completeAll", null, HttpMethod.Post, new Dictionary<string, string>() { { "hej", " AWESOME !" }});dialog = new MessageDialog(t);dialog.Commands.Add(new UICommand("OK"));await dialog.ShowAsync();
AND I GOT A RESPONSE YAY!!
Extra Information
The controllers that you create, i.e. the class needs to end with Controller
, you can have text before but not after. This information was given on a MSDN forum discussion.
If the post
and the get
has the same input the server returns Not found
. Having different inputs solves the issue.
In case of weird Internal Server Error
, i.e. weird you can step through the entire server code all variables that you want to return are initialized, but the client receives the error. Then refer to Internal Server Error - Azure App Service Custom Controller where simple fix to the configuration can solve the issue.
You must have something wrong in your project configuration. I have a working sample here: https://gist.github.com/lindydonna/6fca7f689ee72ac9cd20
After creating the HttpConfiguration
object, call config.MapHttpAttributeRoutes()
. I added the route attribute [Route("api/Test/completeAll")]
and I can confirm that the route is registered correctly.
Try adding this attribute to the ValuesController and check the route.
I found another cause for the 404 errors when it came to use attribute routing.
The code above originally had this in mobileApp.cs:
HttpConfiguration config = new HttpConfiguration(); new MobileAppConfiguration() .UseDefaultConfiguration().MapApiControllers() .ApplyTo(config); config.MapHttpAttributeRoutes(); app.UseWebApi(config);
The config.MapHttpAttributeRoutes() needs to be moved above the .ApplyTo:
HttpConfiguration config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); new MobileAppConfiguration() .UseDefaultConfiguration().MapApiControllers() .ApplyTo(config);