.Net Framework API 控制器无法识别路由属性

.Net Framework API Controller won't recognize Route attributes

我使用 .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 调用,我会收到“未找到”错误。我还需要做些什么来让它识别路线吗?

谢谢, 吉姆

**** 更新 ****

我发现只要用户名不以 .com 等结尾,它就能识别路由。问题是,我使用电子邮件地址作为用户名。有什么地方规定 REST url 不能以 .somthing 结尾吗?有解决办法吗?

问题出在参数的格式上。显然 url 不能以 .com 或其他域后缀结尾。我所做的是将参数转换为 Base64。我创建了这两个扩展函数。

    public static string ToBase64(this string value)
    {
        try
        {
            byte[] bytes = Encoding.UTF8.GetBytes(value);
            return Convert.ToBase64String(bytes);
        }
        catch (Exception)
        {
            return value;
        }
    }

    public static string FromBase64(this string value)
    {
        try
        {
            byte[] bytes = Convert.FromBase64String(value);
            return Encoding.UTF8.GetString(bytes);
        }
        catch(Exception)
        {
            return value;
        }
    }

在控制器中,我做了类似的事情:

[Route("api/ApplicationUser/{username}")]
[ResponseType(typeof(ApplicationUser))]
public IHttpActionResult GetApplicationUser(string username)
{

    username = username.FromBase64();

    ApplicationUser user = new ApplicationUser()

    //Code to populate user.

    return Ok(user);
}

在客户端,我做了类似的事情:

    async Task<ApplicationUser> IApplicationUserService.GetApplicationUser(string username)
    {

        username = username.ToBase64();

        ApplicationUser ret = null;

            var response = await _httpClient.GetAsync($"api/ApplicationUser/{username}");

            if (response.IsSuccessStatusCode)
            {
                ret = await JsonSerializer.DeserializeAsync<ApplicationUser>
                              (await response.Content.ReadAsStreamAsync(), new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
            }

        return ret; ;
    }

干杯, 吉姆