相关疑难解决方法(0)

点字符'.' 在MVC Web API 2中请求诸如api/people/STAFF.45287之类的请求

我试图让工作的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 - 也可以,但不是我的意思在我希望网址接受"点"后,就像下面的示例 - 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.
Run Code Online (Sandbox Code Playgroud)

我已经设置了以下内容:

  1. 控制器"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);
    }    
    
    Run Code Online (Sandbox Code Playgroud)
  2. WebApiConfig.cs

    public static void Register(HttpConfiguration config)
    {
        // …
    Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc-routing asp.net-mvc-4 asp.net-web-api asp.net-web-api2

102
推荐指数
5
解决办法
5万
查看次数