多种put方法可在本地iis中使用,但不能在实时服务器中使用。
Webapi Config:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiById",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiByName",
routeTemplate: "api/{controller}/{action}/{name}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiByActionName",
routeTemplate: "api/{controller}/{action}/{id}/",
defaults: new { id = RouteParameter.Optional }
);
[HttpPut]
public async Task<int> DeleteCity(Guid? Id)
{
City updateCity = await db.City.Where(x => x.Id == Id).FirstOrDefaultAsync();
updateCity.IsActive = 0;
await db.SaveChangesAsync();
return 1;
}
[HttpPut]
[Route("api/city/DeleteCitys/{Id}")]
public async Task<int> DeleteCitys(Guid? Id)
{
City updateCity = await db.City.Where(x => x.Id == Id).FirstOrDefaultAsync();
updateCity.IsActive = 1;
await db.SaveChangesAsync();
return 3;
}
http://freedompg.softwarecubes.com/api/city/76F84F4C-22C8-4EF8-B593-B3605CF9AD14
输出:1是正确的
http://freedompg.softwarecubes.com/api/city/DeleteCitys/76F84F4C-22C8-4EF8-B593-B3605CF9AD14
输出:1错误(调用上一个函数)
答案 0 :(得分:1)
为什么不将RoutePrefix
用作控制器,将Route
用作方法
[RoutePrefix("api/City")]
public class CityController : Controller
{
[HttpPut]
[Route("DeleteCitys")]
public async Task<int> DeleteCitys(Guid[] Id)
{
// Your Code
}
[HttpPut]
[Route("DeleteCitys/{Id}")]
public async Task<int> DeleteCitys(Guid? Id)
{
// Your Code
}
}
答案 1 :(得分:0)
您的代码应该是这样的。您还需要拥有DeleteCity的路线
[HttpPut]
[Route("api/city/{Id}")]
public async Task<int> DeleteCity(Guid? Id)
{
City updateCity = await db.City.Where(x => x.Id == Id).FirstOrDefaultAsync();
updateCity.IsActive = 0;
await db.SaveChangesAsync();
return 1;
}
[HttpPut]
[Route("api/city/DeleteCitys/{Id}")]
public async Task<int> DeleteCitys(Guid? Id)
{
City updateCity = await db.City.Where(x => x.Id == Id).FirstOrDefaultAsync();
updateCity.IsActive = 1;
await db.SaveChangesAsync();
return 3;
}