【发布时间】:2013-02-23 07:00:34
【问题描述】:
我有以下 REST api 实现:
using System.Web.Http;
public class DeploymentsController : ApiController
{
public HttpResponseMessage Post(
string subscriptionId,
string serviceName,
string deploymentSlot,
CreateDeploymentInput input)
{
. . .
}
public HttpResponseMessage Post(
string subscriptionId,
string serviceName,
string deploymentSlot,
UpdateDeploymentStatusInput input)
{
. . .
}
}
这是路由:
config.Routes.MapHttpRoute(
name: "DeploymentSlots",
routeTemplate: "{subscriptionId}/services/hostedservices/{serviceName}/deploymentslots/{deploymentSlot}",
defaults: new
{
controller = "deployments",
});
config.Routes.MapHttpRoute(
name: "Deployments",
routeTemplate: "{subscriptionId}/services/hostedservices/{serviceName}/deployments/{deploymentName}",
defaults: new
{
controller = "deployments"
});
我想为新的“升级”操作添加处理,如下所示:
POST /1d42d489-7a4f-4561-91a3-c2033c31f8c6/services/hostedservices/Mytalk123490193975/deployments/Mytalk12349019/?comp=upgrade HTTP/1.1
Content-Type: application/xml; charset=utf-8
x-ms-version: 2012-08-01
Host: localhost:33344
Content-Length: 340
Expect: 100-continue
Accept-Encoding: gzip, deflate
HTTP/1.1 100 Continue
<UpgradeDeployment xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="ht tp://www.w3.org/2001/XMLSchema-instance"><Mode>auto</Mode><PackageUrl>http://
talkserviceshared.blob.core.windows.net/mycontainer/PackageTXlCaXp0YWxrMTIzNDkwMTkzOTc1
所以我在 ApiController 中添加了一个处理程序:
public HttpResponseMessage Post(
string subscriptionId,
string serviceName,
string deploymentSlot,
UpgradeDeploymentInput input)
{
. . .
}
但是当我使用客户端调用它时,它会抱怨“找到与请求匹配的多个操作”
公共 HttpResponseMessage 帖子(
字符串订阅 ID,
字符串服务名称,
字符串部署槽,
UpdateDeploymentStatusInput 输入)
和
公共 HttpResponseMessage 帖子(
字符串订阅 ID,
字符串服务名称,
字符串部署槽,
UpdateDeploymentStatusInput 输入)
然后我尝试将这些方法合二为一:
public HttpResponseMessage Post(
string subscriptionId,
string serviceName,
string deploymentSlot,
object input)
{
}
并编写了检查运行时类型并采取不同操作的代码。但是这次调用失败了 System.InvalidOperationException
在我开枪之前请帮忙。我没有时间从头开始学习这些东西,我时间紧迫。
【问题讨论】:
标签: asp.net-web-api