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.
ApiControlleris from ASP.NET Web API. AJAX is from AJAX. Please don't remove useful tags.404 (Not found)