【发布时间】:2015-04-21 07:59:56
【问题描述】:
当我尝试在api/objects 上发帖时收到此错误Multiple actions were found that match the request(在此处调用我的代码 sn-p 中的最后一个操作)
我正在使用 web api 1。虽然我有更多控制器和更多围绕 web api 的 Post 操作,但它们都有一些简单的输入,如字符串或 int 或简单和复杂数据的组合。这只是一个仅接收一个复杂对象参数的后操作,因此当然是唯一接收ObjectMainData 模型的操作。
这是我的ObjectsController:
[HttpGet]
public List<ObjectMainData> Get()
{
CompressResponse();
return objectRepository.GetObjects(UserPK);
}
[HttpGet]
public List<ObjectMainData> GetObject(string objectId)
{
CompressResponse();
return objectRepository.GetObjects(UserPK, objectId);
}
[HttpPost] //Update
[OnlineAuthorize]
public ObjectCreatedResponse Post(string objectId,[FromBody]ObjectMainData objekt)
{
// string objectId = objekt.Id;
ObjectCreatedResponse objectIdAndCode = null;
objectIdAndCode = objectRepository.Update(objekt);
return objectIdAndCode;
}
//Insert
[HttpPost, ActionName("Post")]
public ObjectCreatedResponse Spremi(ObjectMainData objekt)
{
string objectId = objekt.Id;
ObjectCreatedResponse objectIdAndCode=null;
if (objectId == null)
{
objectIdAndCode= objectRepository.Insert(objekt,UserPK);
return objectIdAndCode;
}
else
{
objectIdAndCode=objectRepository.Update(objekt);
return objectIdAndCode;
}
这是我的 web api 路由配置的相关部分:
config.Routes.MapHttpRoute(
name: "objects/",
routeTemplate: "api/objects",
defaults: new { controller = "Objects" }
);
config.Routes.MapHttpRoute(
name: "objects/{objectId}",
routeTemplate: "api/objects/{objectId}",
defaults: new { controller = "Objects" },
constraints: new { objectId = @"\d+-\d+" }
);
config.Routes.MapHttpRoute(
name: "api/objects/{objectId}/Attributes",
routeTemplate: "api/objects/{objectId}/Attributes",
defaults: new { controller = "ObjectAttributes" });
config.Routes.MapHttpRoute(
name: "api/objects/{objectId}/Images/{imageId}/Description",
routeTemplate: "api/objects/{objectId}/Images/{imageId}/Description",
defaults: new { controller = "Images", action = "Description" });
config.Routes.MapHttpRoute(
name: "api/objects/{objectId}/images",
routeTemplate: "api/objects/{objectId}/Images",
defaults: new { controller = "Images" });
config.Routes.MapHttpRoute(
name: "Units types api action selected",
routeTemplate: "api/units/types/{objectId}",
defaults: new { controller = "UnitsMisc", action = "Types" });
config.Routes.MapHttpRoute(
name: "api/objects/{objectId}/units/{unitId}",
routeTemplate: "api/objects/{objectId}/units/{unitId}",
defaults: new { controller = "Units" });
config.Routes.MapHttpRoute(
name: "images api",
routeTemplate: "api/objects/{objectId}/Units",
defaults: new { controller = "Units" });
config.Routes.MapHttpRoute(
name: "api/objects/{objectId}/Images/sort",
routeTemplate: "api/objects/{objectId}/Images/sort",
defaults: new { controller = "Sort", action = "Images" });
//SLIKA jedna
config.Routes.MapHttpRoute(
name: "api/objects/{objectId}/Images/{imageId}",
routeTemplate: "api/objects/{objectId}/Images/{imageId}",
defaults: new { controller = "Images" });
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
【问题讨论】:
标签: asp.net-web-api asp.net-web-api-routing