Web Api错误:“找不到与请求URI匹配的HTTP资源”

问题描述 投票:4回答:2

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

场景:

输入用户名和密码后,单击“登录”。我需要获取此信息(用户名和密码),看看它是否存在于我的数据库的users表中。这部分是照顾的。当我将用户名和密码硬编码到我的代码时,它工作正常。如果凭证正确则我得到真实,如果错误则证明错误。现在,我如何从用户那里获取这些值 - 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);
    }
}

有没有更好的方法呢?

c# asp.net-web-api
2个回答
7
投票

您的路由参数名称必须与您的操作参数名称匹配。将您的行动更改为:

public bool Get(string user, string pass)
{
    Authenticate(user, pass);
    return loggedin;
}

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.