作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这里是WebApiConfig.cs中的路由配置:
config.Routes.MapHttpRoute(
name: "DefaultApiPut",
routeTemplate: "api/{controller}",
defaults: new { httpMethod = new HttpMethodConstraint(HttpMethod.Put) }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get, HttpMethod.Post, HttpMethod.Delete) }
);
这是我的 Controller :
public class MyController : ApiController {
[HttpPut]
public void Put()
{
//blah
}
}
不知何故,当客户端发送带有 URL /api/myController/12345
的 PUT 请求时,它仍然映射到 MyController
中的 Put
方法>,我预计会出现类似资源未找到的错误。
如何强制Put
方法只接受不带参数的请求?
提前致谢!
最佳答案
这可以限制路由上的 http 方法:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "LocationApiPOST",
routeTemplate: "api/{orgname}/{fleetname}/vehicle/location",
defaults: new { controller = "location" }
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
);
config.Routes.MapHttpRoute(
name: "LocationApiGET",
routeTemplate: "api/{orgname}/{fleetname}/{vehiclename}/location/{start}",
defaults: new { controller = "location", start = RouteParameter.Optional }
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
...
}
关于c# - 如何在路由中定义 PUT 方法只限于 Controller 中的 Put 方法不带参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12609759/
我是一名优秀的程序员,十分优秀!