问题描述
我是从WCF的Web API新的ASP.NET MVC 4的Web API转换。我有一个UsersController,我想有一个名为身份验证方法。我看到的怎么办GETALL,GetOne,邮政的例子,并删除,但如果我要额外方法加入到这些服务是什么?例如,我的UsersService应该有一个称为身份验证,他们在一个用户名和密码传递方法,但它不工作
I'm converting from the WCF Web API to the new ASP.NET MVC 4 Web API. I have a UsersController, and I want to have a method named Authenticate. I see examples of how to do GetAll, GetOne, Post, and Delete, however what if I want to add extra methods into these services? For instance, my UsersService should have a method called Authenticate where they pass in a username and password, however it doesn't work.
public class UsersController : BaseApiController
{
public string GetAll()
{
return "getall!";
}
public string Get(int id)
{
return "get 1! " + id;
}
public User GetAuthenticate(string userName, string password, string applicationName)
{
LogWriter.Write(String.Format("Received authenticate request for username {0} and password {1} and application {2}",
userName, password, applicationName));
//check if valid leapfrog login.
var decodedUsername = userName.Replace("%40", "@");
var encodedPassword = password.Length > 0 ? Utility.HashString(password) : String.Empty;
var leapFrogUsers = LeapFrogUserData.FindAll(decodedUsername, encodedPassword);
if (leapFrogUsers.Count > 0)
{
return new User
{
Id = (uint)leapFrogUsers[0].Id,
Guid = leapFrogUsers[0].Guid
};
}
else
throw new HttpResponseException("Invalid login credentials");
}
}
我可以浏览到myapi / API /用户/它会调用GETALL,我可以浏览到myapi / API /用户/ 1和它会调用获取,但如果我叫myapi / API /用户/认证?用户名= {0}&放大器;密码= {1},然后它会调用获取(无需验证)和错误:
I can browse to myapi/api/users/ and it will call GetAll and I can browse to myapi/api/users/1 and it will call Get, however if I call myapi/api/users/authenticate?username={0}&password={1} then it will call Get (NOT Authenticate) and error:
参数词典共包含非可空类型'System.Int32'的方法'System.String获取(Int32)已'在'Navtrak.Services.WCF.NavtrakAPI.Controllers.UsersController'参数'ID'无效项。一个可选参数必须是引用类型,可空类型,或声明为可选参数。
我如何可以调用自定义方法的名称,如身份验证?
How can I call custom method names such as Authenticate?
推荐答案
默认情况下,路由配置如下基于REST的约定,这意味着它会只接受GET,POST,PUT和DELETE动作的名字(看在Global.asax中的路由=>默认情况下它不允许你指定的任何动作名称=>它使用HTTP动词调度)。所以,当你发送一个GET请求到 / API /用户/验证
你基本上调用获取(INT ID)
行动并通过 ID =验证
这显然崩溃,因为你的获取动作期待一个整数。
By default the route configuration follows RESTFul conventions meaning that it will accept only the Get, Post, Put and Delete action names (look at the route in global.asax => by default it doesn't allow you to specify any action name => it uses the HTTP verb to dispatch). So when you send a GET request to /api/users/authenticate
you are basically calling the Get(int id)
action and passing id=authenticate
which obviously crashes because your Get action expects an integer.
如果你想有不同的操作名称比标准的人,你可以修改你的路由定义在的Global.asax
:
If you want to have different action names than the standard ones you could modify your route definition in global.asax
:
Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "get", id = RouteParameter.Optional }
);
现在可以导航到 / API /价值/ getauthenticate
来验证用户身份。
Now you can navigate to /api/values/getauthenticate
to authenticate the user.
这篇关于在ASP.NET的Web API自定义的方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!