Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api Error: "No HTTP resource was found that matches the request URI"

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?

like image 914
Program_ming Avatar asked Nov 19 '14 15:11

Program_ming


1 Answers

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;
}
like image 68
Jon Susiak Avatar answered Oct 05 '22 14:10

Jon Susiak