我想让其运行的URL样式为:http://somedomain.com/api/people/staff.33311(就像LAST.FM等网站允许在其RESTful和WebPage URL中使用各种符号一样,例如“http://www.last.fm/artist/psy'aviah”是LAST.FM的有效URL)。
以下情况是可以工作的: - http://somedomain.com/api/people/ - 返回所有人 - http://somedomain.com/api/people/staff33311 - 也可以,但这不是我想要的 我想要的是url接受“点”,如下例所示: - http://somedomain.com/api/people/staff.33311 - 但是这个给了我一个
。HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
我已经设置了以下内容:
控制器 "PeopleController"
public IEnumerable<Person> GetAllPeople()
{
return _people;
}
public IHttpActionResult GetPerson(string id)
{
var person = _people.FirstOrDefault(p => p.Id.ToLower().Equals(id.ToLower()));
if (person == null)
return NotFound();
return Ok(person);
}
WebApiConfig.cs文件
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
我已经尝试了这篇博客文章中的所有提示:http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx,但仍然无法解决。我也认为这样做相当繁琐,不知道是否有另一种更好、更安全的方法。
我们的内部ID是像这样的,所以我们需要找到一种方法来适应".",最好是像点一样,但如果需要的话,我也可以接受URL的替代建议...