Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web Api错误:";找不到与请求URI匹配的HTTP资源;_C#_Asp.net Web Api - Fatal编程技术网

C# Web Api错误:";找不到与请求URI匹配的HTTP资源;

C# Web Api错误:";找不到与请求URI匹配的HTTP资源;,c#,asp.net-web-api,C#,Asp.net Web Api,我正在尝试创建一个Web Api conroller,允许我的用户使用他们的凭据(用户名、密码)登录到该站点 场景: public bool Get(string txtLoginId, string txtPassword) { Authenticate(txtLoginId, txtPassword); return loggedin; } 键入用户名和密码后,单击登录。我需要获取此信息(用户名和密码),并查看它是否存在于数据库的us

我正在尝试创建一个Web Api conroller,允许我的用户使用他们的凭据(用户名、密码)登录到该站点

场景:

    public bool Get(string txtLoginId, string txtPassword)
    {
        Authenticate(txtLoginId, txtPassword);
        return loggedin;
    }
键入用户名和密码后,单击登录。我需要获取此信息(用户名和密码),并查看它是否存在于数据库的users表中。这部分已经处理好了。当我将用户名和密码硬编码到我的代码中时,它可以正常工作。如果凭据正确,则为true;如果凭据不正确,则为false。现在,我如何从用户那里获得这些值——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;
}

检查路由参数名称和方法参数名称是否匹配

谢谢您的帮助。现在,要发布它,我需要使用一个名为post的函数?我想发布布尔值。我建议您在。他们应该回答你的大部分问题。当然,他们会回答的。谢谢。亲爱的@Simon,请用例子解释更多。@desmati,在本例中使用基于属性的路由,然后在方法中使用:[Route(“api/yourcontroller/{name})]:public IHttpActionResult GetByName(name)注意[Route]属性中的“name”参数如何与GetByName()方法的参数匹配