按电子邮件而不是 ID 搜索

Search by email not id

本文关键字:ID 搜索 电子邮件      更新时间:2023-09-26

我们知道 Asp.net Web API 2 具有按 id 搜索的 api/get/5 方法。我需要通过电子邮件搜索,我在这里找到了这个,我需要一个简单的例子。我还研究了 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;
         });
谢谢

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

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

我猜你在$http电话中忘记了'/'

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

和阿斯普:-

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

希望它有助于不确定:)