调用WEB API时找不到资源错误

时间:2019-01-28 09:38:18

标签: asp.net api asp.net-web-api

我创建了一个WEB API来获取列表。我创建了另一个登录正常的API。但是每当调用提供我一些列表的API时,它将显示错误为 未找到与请求URI'http://localhost:85476/API/EmployeeMobile/GetEmployeeForMobile 相匹配的HTTP资源, 在与请求匹配的控制器“ EmployeeMobile”上未找到任何操作。

我的控制器

 public class EmployeeMobileController : ApiController
    {

        private PCommon _pCommon;
        IEmployeeBusiness _employeeBusiness;
        Mapper _mapper;
        public EmployeeMobileController()
        {
            _pCommon = new PCommon();
            _employeeBusiness = new EmployeeBusiness();

            _mapper = new Mapper();
        }

        [HttpPost]

        public string GetEmployeeForMobile(string userID, string connectionString)
        {
            DataSet dsEmployee = _employeeBusiness.GetAllEmployeeForMobile(Guid.Parse(userID), connectionString);
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            object routes_list = (object)json_serializer.DeserializeObject(JsonConvert.SerializeObject(dsEmployee.Tables[0]));
            return JsonConvert.SerializeObject(routes_list);
        }
    }

我的WebApiConfig.cs

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );


        }
    }

我的PostMan呼叫详细信息

http://localhost:61557/API/EmployeeMobile/GetEmployeeForMobile

Body: {"userID":"fc938df0-373c559f","connectionString":"Data\tSource=WIN\\MSSQLSERVER2016;Initial\tCatalog=PDefault;User\tID=db;Password=db123"}

请帮助我解决此问题。

1 个答案:

答案 0 :(得分:1)

请注意,WebAPI不支持多个POST参数的绑定。您正在像POST方法那样定义GET。更改它以返回单个模型

您的模型应如下所示。

 public class Input
    {
        public string uSerID { get; set; }
        public string connectionString { get; set; }
    }

现在更改

之类的POST方法
 [HttpPost]
 public string GetEmployeeForMobile(Input request)
  {
   //Your implementation here
  }

JSON的帖子应该如下所示。

{uSerID:"abc", connectionString :"xyz"}