【问题标题】:Search by email not id通过电子邮件而不是 ID 搜索
【发布时间】:2015-06-20 15:35:44
【问题描述】:

我们知道 Asp.net Web API 2 有 api/get/5 方法用于通过 id 进行搜索。我需要通过电子邮件搜索,我发现这个here 我需要一个简单的例子。我还研究了 Angularjs 路由,我发现这个主题需要时间,我今天必须完成这个。

我的 api/users/5 控制器的一部分。

// GET: api/users/5         get user by id
    public Users Get(int id)
    {
        Users user = db.Users.Find(id);
        if (user == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        return user;
    }

我的 JS 代码

 $http.get('/api/users'+ $scope.Email)
         .success(function (response) {
             $scope.users = response;
         });
谢谢

【问题讨论】:

  • 404(未找到)。虽然电子邮件在数据库中

标签: javascript angularjs rest asp.net-web-api angularjs-routing


【解决方案1】:

尝试将电子邮件作为参数传递。

public Users Get(string Email)
    {
        Users user = db.Users.Find(Email);
        if (user == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        return user;
    }

【讨论】:

  • 404(未找到)。虽然电子邮件在数据库中
  • 尝试为电子邮件检查区分大小写的 ToLower() 或调试代码并检查问题出现的位置。
【解决方案2】:

我猜你在 $http 调用中忘记了“/”

$http.get('/api/users/'+ $scope.Email)
         .success(function (response) {
             $scope.users = response;
         });

和 Asp :-

public Users Get(string Email)
    {
        Users user = db.Users.Find(Email);
        if (user == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        return user;
    }

希望对不确定有帮助:)

【讨论】:

  • 感谢您的回复。 404(未找到)。虽然电子邮件在数据库中
猜你喜欢
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多