如何将多个字符串参数传递到Web API

时间:2014-09-05 22:14:16

标签: asp.net-web-api

我试图编写一个简单的WebAPI控制器来验证userId和密码。这两个参数都是字符串,这有点令人沮丧,因为这似乎总是如此困难。

Controller代码如下所示:

[Route("api/User/{userId}, {password}"), HttpGet]
public bool IsPasswordCorrect(string userId, string password)
    {
        UserModel sm = new UserModel(userId);
        bool rtn = sm.Password == password;
        return rtn;
    }

调用代码如下:

public bool IsPasswordValid(string userId, string password)
    {
        RestRequest request = new RestRequest("IsPasswordCorrect/{userId}, {password}", Method.GET);
        request.AddUrlSegment("userId", userId);
        request.AddUrlSegment("password", password);
        RestClient restClient = new RestClient(Service.Location);

        var response = restClient.Execute<bool>(request);
        bool rtn = (bool)response.Data;

        return true;
    }

我无法获取调用代码来访问控制器代码。感谢

1 个答案:

答案 0 :(得分:0)

您已将路线定义为:

  GET {host}/api/User/{userId}, {password}

但看起来你称之为:

 GET {Service.Location}/IsPasswordCorrect/{userId}, {password}

您应该按属性中定义的路由调用Web API资源,而不是按操作名称调用,这将导致404。