Asp.net mvc 找不到Web Api路由

Asp.net mvc 找不到Web Api路由,asp.net-mvc,asp.net-web-api,asp.net-mvc-routing,Asp.net Mvc,Asp.net Web Api,Asp.net Mvc Routing,我已经研究了很多关于这个话题的问题。说到MVC3,我并不是无精打采的,但就我个人而言,我不明白为什么我在MVC4路由中的Web Api失败了 Fiddler2显示服务器响应404(未找到) 在App_Start/WebApiConfig.cs中 config.Routes.MapHttpRoute( name: "DefaultApiGet", routeTemplate: "api/{domain}/{controller}",

我已经研究了很多关于这个话题的问题。说到MVC3,我并不是无精打采的,但就我个人而言,我不明白为什么我在MVC4路由中的Web Api失败了

Fiddler2显示服务器响应404(未找到)

在App_Start/WebApiConfig.cs中

config.Routes.MapHttpRoute(
            name: "DefaultApiGet", 
            routeTemplate: "api/{domain}/{controller}", 
            defaults: new { action = "Get" },
            constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
            );

        config.Routes.MapHttpRoute(
            name: "ApiPostWithAction",
            routeTemplate: "api/{domain}/{controller}/{action}",
            defaults: new { action = "Post" },
            constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
            );

        config.Routes.MapHttpRoute(
            name: "DefaultApiPost",
            routeTemplate: "api/{domain}/{controller}", 
            defaults: new { action = "Post" }, 
            constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
            );

        config.Routes.MapHttpRoute(
                  name: "ControllerAndId",
                  routeTemplate: "api/{domain}/{controller}/{id}",
                  defaults: null,
                  constraints: new { id = new GuidConstraint() } // Only Guids 
                  );
在控制器/Api/[ModelName]控制器中

    [ActionName("CustomerLogin")]
    [HttpPost]
    //public HttpResponseMessage CustomerLogin(string username, string password)
    public HttpResponseMessage PostCustomerLogin(string username, string password)
    {
来自客户端的调用路径

var url = "api/[client_specific_name]/Customer/CustomerLogin";
var userData = new {username:[someusername], password:[somepassword]};
var defaultSettings = {
        type: 'POST',
        data: userData
    };
// SENT TO SERVER VIA AJAX ALONG WITH THE DATA ABOVE
找不到我丢失的东西


有什么建议吗?

一个解决方案是引入一个对象:

public class AuthData
{
    public string UserName { get; set; }
    public string Password { get; set; }
}
然后更改方法的签名

[ActionName("CustomerLogin")]
[HttpPost]
//public HttpResponseMessage CustomerLogin(string username, string password)
public HttpResponseMessage PostCustomerLogin(AuthData data)
{
这将正确找到
url=“api/[client\u specific\u name]/Customer/CustomerLogin”的方法以及大多数情况下,绑定来自请求主体的数据

有关更多详细信息,请阅读此处:,您将发现param绑定的默认值为:

  • 简单类型取自URI
  • 复杂类型取自请求主体

您在请求正文中发送数据,没有url参数
用户名
密码

,这绝对是正确的。我想我只是不够聪明。我以前读过那篇文章,并没有从中得出解决方案…:-(……很抱歉,没有足够的代表积分给你打分。如果有帮助,那就太好了(好问题;)