2014-09-05 52 views
0

我正在尝试编写一个简单的WebAPI控制器来验证userId和密码。这两个参数都是字符串,这似乎总是如此困难,这有点令人沮丧。如何将多个字符串参数传递到WebAPI

的控制器代码如下所示:

[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; 
    } 

我不能得到调用代码访问控制器代码。由于

回答

0

您已经定义为路线:

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

,但看起来像你调用它为:

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

应该通过属性定义的路由调用Web API资源不通过行动名称,因为这将导致404。

相关问题