我在webapiconfig.cs文件中编写了以下配置代码,用于路由多个fintion。在get方法中,我发现多个操作与请求匹配:System.String GetJobDetails(System.String)错误。
Webapiconfig.cs代码
config.Routes.MapHttpRoute(
name: "RendererAPi",
routeTemplate: "shared/{controller}/{id}",
defaults: new {id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "RendererAPiStatus",
routeTemplate: "shared/{controller}/{id}/status",
defaults: new { action = "getJobStatus", id = RouteParameter.Optional }
);
我的控制器代码:
//post shared/rendererjob
[HttpPost]
public string createRendererJob(HttpRequestMessage request)
{
return "teststring";
}
//put shared/rendererjob/renderGUID
[HttpPut]
public string DoPutRequest([FromUri(Name="id")]string renderGUID)
{
return renderGUID;
}
//get shared/rendererjob/renderGUID
[HttpGet]
public string GetJobDetails([FromUri(Name = "id")]string renderGUID)
{
return renderGUID;
}
//get shared/rendererjob/renderGUID/status
[HttpGet]
public HttpResponseMessage getJobStatus([FromUri(Name = "id")]string jobid)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
string uri = Url.Link("RendererAPiStatus", new { id = jobid });
response.Headers.Location = new Uri(uri);
return response;
}
3 URL工作正常,// get shared / rendererjob / renderGUID [HttpGet]无法正常工作,并在浏览器中收到多重操作错误。
任何人请建议我这个。 注意:Route方法在MVC4 VS2012中不起作用,并且无法将任何补丁安装到我的系统中。
答案 0 :(得分:0)
问题是你有两个具有相同签名的GET方法,所以你需要以某种方式区分它们。尝试向使用RendererAPi路由的方法添加默认操作名称:
[HttpPost]
[ActionName("Default")]
public string createRendererJob(HttpRequestMessage request)
{
return "teststring";
}
//put shared/rendererjob/renderGUID
[HttpPut]
[ActionName("Default")]
public string DoPutRequest(string id)
{
return renderGUID;
}
//get shared/rendererjob/renderGUID
[HttpGet]
[ActionName("Default")]
public string GetJobDetails(string id)
{
return renderGUID;
}
//get shared/rendererjob/renderGUID/status
[HttpGet]
public HttpResponseMessage getJobStatus(string id)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
string uri = Url.Link("RendererAPiStatus", new { id = id });
response.Headers.Location = new Uri(uri);
return response;
}
然后按如下方式更改RendererAPi路线:
config.Routes.MapHttpRoute(
name: "RendererAPi",
routeTemplate: "shared/{controller}/{id}",
defaults: new {action = "Default", id = RouteParameter.Optional }
);
顺便说一下,您不需要为字符串包含[FromUri]属性。