我正在尝试创建一个Web Api控制器,允许我的用户使用他们的凭据(用户名,密码)登录到站点。
场景:
键入用户名和密码后,单击登录。我需要获取此信息(用户名和密码),并查看它是否存在于数据库的用户表中。这部分得到了照顾。当我将用户名和密码硬编码到我的代码中时,它工作正常。如果凭据正确,我会得到真,如果不正确,我会得到假。现在,如何从用户那里获取此值 - URL 或我不知道的其他方式?
目前,我收到以下错误:
{"Message":"No HTTP resource was found that matches the request URI
'http://localhost:4453/api/login/username/password'.","MessageDetail":"No
action was found on the controller 'Login' that matches the request."}
请记住,这是我看Web Api的第二天。
这是我的代码,我不确定出了什么问题。
控制器:
public bool Get(string txtLoginId, string txtPassword)
{
Authenticate(txtLoginId, txtPassword);
return loggedin;
}
WebApiConfig
public static class WebApiConfig
{
public static void Authentication(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "AuthenticationApi",
routeTemplate: "api/{controller}/{user}/{pass}",
defaults: new { user = RouteParameter.Optional, pass = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
有没有更好的方法?
路由参数名称必须与操作参数名称匹配。将您的操作更改为:
public bool Get(string user, string pass)
{
Authenticate(user, pass);
return loggedin;
}
检查路由参数名和方法参数名是否匹配