fuzzlog
fuzzlog

Reputation: 131

Web Api route not found

I have already researched many of the questions regarding this topic. I'm no slouch when it comes to MVC3 but for the life of me I can't figure out why my Web Api in MVC 4 routing is failing.

Fiddler2 shows a server response 404 (No Found)

IN 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 
                  );

IN Controllers/Api/[ModelName]Controller

    [ActionName("CustomerLogin")]
    [HttpPost]
    //public HttpResponseMessage CustomerLogin(string username, string password)
    public HttpResponseMessage PostCustomerLogin(string username, string password)
    {

CALL PATH FROM CLIENT

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

Can't find the what I'm missing.

Any suggestions?

Upvotes: 2

Views: 3856

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123901

One solution could be to introduce an object:

public class AuthData
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

And then change the signature of your Method

[ActionName("CustomerLogin")]
[HttpPost]
//public HttpResponseMessage CustomerLogin(string username, string password)
public HttpResponseMessage PostCustomerLogin(AuthData data)
{

That will correctly find the Method for url = "api/[client_specific_name]/Customer/CustomerLogin"; and mostly, bind the data from the request-body.

For more details read here:Routing and Action Selection, where you will find that the defaults for param binding is:

  • Simple types are taken from the URI.
  • Complex types are taken from the request body.

And you are sending data inside the request-body, no url params username and password

Upvotes: 3

Related Questions