【发布时间】:2020-11-11 09:00:37
【问题描述】:
我使用 .Net Framework 创建了一个 API 控制器,如下所示:
public class ApplicationUsersController : ApiController
{
[Route("api/ApplicationUser/{username}/{password}")]
[ResponseType(typeof(ApplicationUser))]
public IHttpActionResult GetApplicationUser(string username, string password)
{
ApplicationUser user = new ApplicationUser()
//Code to populate user.
return Ok(user);
}
[Route("api/ApplicationUser/{username}")]
[ResponseType(typeof(ApplicationUser))]
public IHttpActionResult GetApplicationUser(string username)
{
ApplicationUser user = new ApplicationUser()
//Code to populate user.
return Ok(user);
}
// PUT: api/ApplicationUsers/5
[Route("api/ApplicationUser/{username}")]
[ResponseType(typeof(void))]
public IHttpActionResult PutApplicationUser(string username, ApplicationUser ApplicationUser)
{
//Code to update user
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/ApplicationUsers
[Route("api/ApplicationUser")]
[ResponseType(typeof(ApplicationUser))]
public IHttpActionResult PostApplicationUser(ApplicationUser ApplicationUser)
{
//Code to create new user
return Ok(ApplicationUser);
// return CreatedAtRoute("api/ApplicationUser/{username}", new { username = ApplicationUser.UserName }, ApplicationUser);
}
// DELETE: api/ApplicationUsers/5
[Route("api/ApplicationUser/{username}")]
[ResponseType(typeof(ApplicationUser))]
public IHttpActionResult DeleteApplicationUser(string username)
{
//Code to populate user then delete the record.
return Ok(user);
}
}
当我对 api/ApplicationUser/{username}/{password} 进行 Get 调用时,它工作正常。如果我对 api/ApplicationUser 进行 Post 调用,它可以正常工作。如果我对 api/ApplicationUser/{username} 进行 Get、Put 或 Delete 调用,我会收到“未找到”错误。我还需要做些什么来让它识别路线吗?
谢谢, 吉姆
**** 更新****
我发现只要用户名不以 .something 结尾,例如 .com,它就会识别路由。问题是,我使用电子邮件地址作为用户名。 REST url 不能以 .somthing 结尾的规则是否存在?有没有办法解决这个问题?
【问题讨论】:
标签: asp.net-web-api asp.net-web-api2 asp.net-web-api-routing