【问题标题】:.Net Framework API Controller won't recognize Route attributes.Net Framework API 控制器无法识别路由属性
【发布时间】: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


    【解决方案1】:

    问题在于参数的格式。显然,网址不能以 .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; ;
        }
    

    干杯, 吉姆

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 1970-01-01
      • 2021-08-12
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-13
      • 1970-01-01
      相关资源
      最近更新 更多