1. controller / action / id webapi path in configuration, open the file [App_Start] - [WebApiConfig]
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } );
2. Use Direct [HttpPost] filter, add method implemented action:
[HttpPost] // POST api/<controller> public string AdminLogin(string userName,string passWord) { return ""; }
Problems: the front end of the "type" was changed to "Post" way, in any case less than the acquisition method.
3. Solution one:
① adding transit object class, used to store parameters.
public class AdminLogin { public string UserName { get; set; } public string PassWord { get; set; } }
② modify the action parameters.
[HttpPost] // POST api/<controller> public string AdminLogin([FromBody]AdminLogin adminLogin) { return adminLogin.UserName; }
③ Method distal call:
$.ajax({
type: "POST", url: AdminApi.AdminLogin,
data: {UserName:"xxx",PassWord:"XXX"},
success: function (data) {
//处理方法
}
});
4. Solution two:
① modify parameters for the action and dynamic method call:
[HttpPost] // POST api/<controller> public string AdminLogin([FromBody]dynamic adminLogin) { string userName = adminLogin.UserName; string passWord = adminLogin.PassWord; return userName; }
② modify the front invocation:
UserName {obj = var: username, PassWord: password};
// call api authentication method
$ .ajax ({
type: "the POST", URL: AdminApi.AdminLogin, Data: the JSON.stringify (obj), contentType: "file application / JSON ",
Success: function (Data) {
// implementation internal
}
});