【发布时间】:2017-01-03 10:19:21
【问题描述】:
这里我正在尝试使用 [Route] 属性调用 WebApi 控制器
为什么http://localhost:57997/Hello/Jan/1 不是配置路由
而http://localhost:57997/Hello/Jan正在获取数据
using a = System.Web.Http;
[a.Route("Hello/Jan")]
public IEnumerable<Department> GetDepartmets()
{
var x = pro.GetDept();
return x.ToList();
}
[a.Route("Hello/Jan/{id?}")]
public HttpResponseMessage GetDepartmets(int id)
{
if (id != null)
{
var x = pro.GetDeptById(id);
return Request.CreateResponse(HttpStatusCode.OK, x);
}
else
return Request.CreateResponse(HttpStatusCode.NotFound);
}
【问题讨论】:
-
添加一个路由约束,看看它是否解决了问题
[a.Route("Hello/Jan/{id:int?}")]。您可能还需要包含 Http{Verb} 即:[a.HttpGet]。尽管约定应该根据动作名称来选择它 -
你能展示一个更完整的控制器版本吗?
标签: c# asp.net-web-api asp.net-web-api-routing