0
我需要在以下url中提到的{ID}中为web api服务添加数据注释,以接受最大长度为7,否则我应该抛出自定义异常。我没有任何使用maxlength属性的模型类。感谢你的帮助。为查询字符串获得的值添加数据注释
http://xyz.com/{ID}
我需要在以下url中提到的{ID}中为web api服务添加数据注释,以接受最大长度为7,否则我应该抛出自定义异常。我没有任何使用maxlength属性的模型类。感谢你的帮助。为查询字符串获得的值添加数据注释
http://xyz.com/{ID}
什么,你可以尝试做如下,从你的问题有URL假设:
routes.MapRoute(
"Default", // Route name
"{id}", // URL with parameters
new { controller = "Home", action = "Index" }, // Parameter defaults,
new { id = @"\d{4}" } //Constraint
);
routes.MapRoute(
"DefaultError", // Route name
"{id}", // URL with parameters
new { controller = "Home", action = "ThrowError" }, // Parameter defaults,
new { id = @"\d{5,}" } //Constraint
);
,并在你的控制器ThrowError(int id)
方法,抛出你想要的错误或重定向到错误页面。
为什么不在WebAPI方法中包含处理程序?
public SomeModel Get(int id)
{
if (id != null && id <= 7)
return something();
throw new HttpResponseException(HttpStatusCode.{BadRequest|Forbidden|SomethingElse});
}
尽管为什么要抛出自定义异常,那么路由约束是要走的路,那不是非常RESTful。返回一个HTTP状态码。 – 2013-04-30 19:09:07