我试图让工作的 URL 是一个样式: http://somedomain.com/api/people/staff.33311(就像网站 LAST.FM 允许所有种类的标志在他们的 RESTFul & WebPage 网址,例如“ http://www.last.fm/artist/psy'aviah”是一个有效的网址 LAST.FM)。
有效的方案如下: - 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);
}
The 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 }
);
}
I already tried following all the tips of this blogpost http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx but it still won't work.. I also think it's quite tedious and I wonder if there isn't another, better and more secure way.
We have our Id's internally like this, so we're going to have to find a solution to fit the dot in one way or another, preferably in the style of "." but I'm open to alternative suggestions for urls if need be...