prawn
prawn

Reputation: 2653

web api route with optional parameter

I am currently playing around with some things...According to this link, I need to construct a route that is open to the following format

webServiceURL/version/devices/deviceLibraryIdentifier/registrations/passTypeIdentifier?passesUpdatedSince=tag

so I defined the route like so

 config.Routes.MapHttpRoute(
       name: "DefaultApi3",
       routeTemplate: "{version}/devices/{deviceLibraryIdentifier}/registrations/{passTypeIdentifier}/{passesUpdatedSince}",
       defaults: new { controller = "SerialNumbers", action = "GET", passesUpdatedSince = RouteParameter.Optional }
           );

However, the following route fails for the url

http://localhost/v1/devices/24358235235loji200/registrations/pass.com.mypass?passesUpdatedSince=12a512

How can I configure the route so that the above url can reach my controller?

My controller looks like

[HttpGet]
public HttpResponseMessage Get(string passesUpdatedSince ="")
{
        //do stuff
}

UPDATE

Thanks to the comments, I've made the following changes.

the route

config.Routes.MapHttpRoute(
           name: "DefaultApi3",
           routeTemplate: "v1/devices/{deviceLibraryIdentifier}/registrations/{passTypeIdentifier}",
           defaults: new { controller = "SerialNumbers", action = "GET" }
        );

My controller is as follows

public HttpResponseMessage Get(string deviceLibraryIdentifier,
                           string passTypeIdentifier,
                           string passesUpdatedSince = "")
    {
          //do stuff
    }

According to the Apple docs, is it right to assume the following the webservice calls could look like

http://localhost:31472/v1/devices/23lk5235232oijlk/registrations/pass.com.mypass http://localhost:31472/v1/devices/23lk5235232oijlk/registrations/pass.com.mypass?passesUpdatedSince=159025

as these are returning 404.

These, however, do work. http://localhost:31472/v1/devices/23lk5235232oijlk/registrations/pass.com.mypass/?passesUpdatedSince=1415l http://localhost:31472/v1/devices/23lk5235232oijlk/registrations/pass.com.mypass/

So would there be a way to get it to work without the presence of the / near the end of the url?

It does look like the device is unable to recognize the route. I get the following message

Get serial #s task (for device 2523ff2fswtsfdh6544, pass type pass.com.mypass, last updated (null); with web service url https://weburl) encountered error: Unexpected response code 404

Upvotes: 2

Views: 1902

Answers (2)

prawn
prawn

Reputation: 2653

Because part of the URI had periods in it (pass.com.mypass), this always returned a 404

I had to add the

<modules runAllManagedModulesForAllRequests="true" />

in my web.config. And after that, everything worked as expected

Upvotes: 1

ps2goat
ps2goat

Reputation: 8475

For the route, try:

config.Routes.MapHttpRoute(
       name: "DefaultApi3",
       routeTemplate: "{version}/devices/{deviceLibraryIdentifier}/registrations/{passTypeIdentifier}",
       defaults: new { controller = "SerialNumbers", action = "GET" }
 );

Note that you should actually have a hard-coded value where {version} is, according to the link you gave us (https://developer.apple.com/library/ios/documentation/PassKit/Reference/PassKit_WebService/WebService.html#//apple_ref/doc/uid/TP40011988-CH0-SW4).

A hard-coded version would look like this:

config.Routes.MapHttpRoute(
           name: "DefaultApi3",
           routeTemplate: "v1/devices/{deviceLibraryIdentifier}/registrations/{passTypeIdentifier}",
           defaults: new { controller = "SerialNumbers", action = "GET" }
     );

Your controller action also needs to be able to accept all parameters of the route:

[HttpGet]
public HttpResponseMessage Get(string deviceLibraryIdentifier, 
                               string passTypeIdentifier, 
                               string passesUpdatedSince ="")
{
        //do stuff
}

Upvotes: 0

Related Questions