带有可选参数的web api路由

问题描述 投票:2回答:2

我正在玩一些东西...根据this link,我需要构建一个开放到以下格式的路线

webServiceURL /版本/设备/ deviceLibraryIdentifier /注册/ passTypeIdentifier?passesUpdatedSince =标签

所以我定义了这样的路线

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

但是,url的以下路由失败

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

如何配置路由以便上面的URL可以到达我的控制器?

我的控制器看起来像

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

UPDATE

感谢评论,我做了以下更改。

路线

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

我的控制器如下

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

根据Apple文档,假设以下Web服务调用看起来是正确的

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

因为这些正在返回404。

然而,这些确实有效。 http://localhost:31472/v1/devices/23lk5235232oijlk/registrations/pass.com.mypass/?passesUpdatedSince=1415l http://localhost:31472/v1/devices/23lk5235232oijlk/registrations/pass.com.mypass/

那么,如果没有靠近网址末尾的/,有没有办法让它工作?

看起来设备无法识别路线。我收到以下消息

获取serial #s任务(对于设备2523ff2fswtsfdh6544,传递类型pass.com.mypass,最后更新(null);使用web服务url https://weburl)遇到错误:意外响应代码404

c# asp.net-web-api asp.net-web-api2
2个回答
0
投票

对于路线,请尝试:

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

请注意,根据您给我们的链接({version}),您实际上应该有https://developer.apple.com/library/ios/documentation/PassKit/Reference/PassKit_WebService/WebService.html#//apple_ref/doc/uid/TP40011988-CH0-SW4的硬编码值。

硬编码版本如下所示:

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

您的控制器操作还需要能够接受路径的所有参数:

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

0
投票

因为URI的一部分有句号(pass.com.mypass),所以总是返回404

我不得不加上

<modules runAllManagedModulesForAllRequests="true" />

在我的web.config中。在那之后,一切都按预期工作

© www.soinside.com 2019 - 2024. All rights reserved.