Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ApiController in Mobile App

I have created a backend service using Azure Mobile App. Offline sync works fine with mobile clients using provided SDK. All the controllers that I have used are TableController. Now I want to add a simple Web API 2 controller ApiController, that will not be used by mobile clients. Here is a simple ApiController that I have added to Controllers folder:

public class SimpleController : ApiController
{
    public string Get()
    {
        return "Hello";
    }
}

But the controller is never hit. If I add [MobileAppController] attrebute to the controller, it works but now it asks for additional headers in the request (I guess this headers are sent by client SDK):

{"message":"No API version was specified in the request, this request needs to specify a ZUMO-API-VERSION of '2.0.0'. For more information and supported clients see: http://go.microsoft.com/fwlink/?LinkId=690568#2.0.0"}

But I do not need this additional functionality here - I just want my service to respond to simple GET requests. Although the guide states that it is not necessary to decorate the class:

Any controller that does not have MobileAppControllerAttribute applied can still be accessed by clients, but it may not be correctly consumed by clients using any Mobile App client SDK.

I can not achieve this. Am I missing something?

Thanks

like image 333
Maxim Lazarev Avatar asked Mar 17 '26 16:03

Maxim Lazarev


1 Answers

I have figured out how to use both types of controllers.

Just add a call to config.MapHttpAttributeRoutes(); in your StartUp.ConfigureMobileApp method like this:

public static void ConfigureMobileApp(IAppBuilder app)
{
    HttpConfiguration config = new HttpConfiguration();

    //For more information on Web API tracing, see http://go.microsoft.com/fwlink/?LinkId=620686 
    config.EnableSystemDiagnosticsTracing();

    config.MapHttpAttributeRoutes(); //Add this line

    new MobileAppConfiguration()
        .UseDefaultConfiguration()
        .ApplyTo(config);

    ... //rest of the code
}

And then decorate your controller with custom route:

[Route("api/Simple")] //add this
public class SimpleController : ApiController
{
    public string Get()
    {
        return "Hello";
    }
}
like image 171
Maxim Lazarev Avatar answered Mar 20 '26 18:03

Maxim Lazarev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!