2

Here is my problem, I want to receive the Username and Password of the client with POST request.

The code seems simple but doesn't work

The LoginController.cs :

public class LoginController : ApiController
{
    [HttpPost]
    [ActionName("Login")]
    [Route("api/{controller}")]
    public HttpResponseMessage Login([FromBody] LoginJson json)
    {
        return Request.CreateResponse(HttpStatusCode.OK);
    }
}

The LoginJson.cs form :

public class LoginJson
{
    public string Username { get; set; }
    public string Password { get; set; }
}

The ajax request with jQuery, i don't want to change the url because i want to use 3 urls /api/Login, /api/Method1 and /api/Method2 for 3 different controllers:

$.ajax({
        url: '/api/Login',
        type: 'POST',
        dataType: "json",
        contentType: "application/json, charset=utf-8",
        data: JSON.stringify({
            Username: username,
            Password: password,
        }),
        ...
});

The route for API, in Global.asax.cs :

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalFilters.Filters.Add(new HandleErrorAttribute());
    RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    RouteTable.Routes.MapRoute(...)


    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}",
        defaults: new {action = RouteParameter.Optional}
    );
}

I get error 404 (Not Found). I will change the Global.asax.cs file.

4
  • ApiController is from ASP.NET Web API. AJAX is from AJAX. Please don't remove useful tags. Commented Oct 26, 2018 at 15:11
  • Seems like this is perfect for attribute routing. blogs.msdn.microsoft.com/webdev/2013/10/17/… Commented Oct 26, 2018 at 15:50
  • @mason Can you help me by suggesting your answer please? I try different possibilities like tips in your link but i still have 404 (Not found) Commented Oct 26, 2018 at 16:01
  • The documentation I provided clearly describes how to set up attribute routing. Commented Oct 26, 2018 at 16:41

2 Answers 2

3

You are only calling the controller with no action and since there is no default action defined you'll get a 404 error.

In the jQuery you can do:

url: '/api/Login/Login'

Or change the routing by either putting this tag:

[Route("api/login")]

Or in the RouteConfig.cs, this should be done before the other routes are set including the generic one.

routes.MapRoute("Login", "Login/{action}",
        defaults: new { controller = "Login", action = "Login" });
Sign up to request clarification or add additional context in comments.

5 Comments

I don't want to change the url from jQuery, the [ActionName("api/login")] don't work and you forgot the last option
@Rodrigue I've added in how to add it in RouteConfig.cs
You mean set default controller to Login ? It's a bad idea. It'll just bring the problem back later with other Controller class. If I have 3 different controllers; i just want to have /api/Login, /api/Action1, /api/Action2 as urls for my POST requests. Anyway no solution works
That's not setting the default controller to Login for everything only when you do /Login. So other controller will work the same as they do. And forgot to mention you will have to put that before the other routes in RouteConfig otherwise the generic one will take over.
Sorry but your answer is still not enough for me, I can't get another error code than 404 (Not found). Can I ask you for a more complete answer please?
0

There is no api/login

You can do like that:

[ActionName("api/login")]
public HttpResponseMessage Login([FromBody] LoginJson json)
{
    return Request.CreateResponse(HttpStatusCode.OK);
}

1 Comment

Not working. I would like to use the urls: /api/Login, /api/Method1 and /api/Method2 for 3 different controllers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.