I am trying to create a Web Api conroller that allows my users to log-in to the site using their credentials (username, password).
Scenario:
After typing the username and password, click login. I need to get this info(username and password) and see if it exists in the users table in my db. this part is taken care of. When I hard code the username and password to my code it works fine. I get true if the credentials correct and false when incorrect. Now, how can I get this values from the user - the URL or perhaps another way that I am not aware of?
Currently, I get the following error:
{"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."}
Please keep in mind that this is my second day looking at Web Api.
Here is my code I am not sure what went wrong.
Controller:
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);
}
}
Is there a better way to do it?
Your route parameter names have to match your action parameter names. Change your Action to:
public bool Get(string user, string pass)
{
Authenticate(user, pass);
return loggedin;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With