I have a .NETCore 2.1 web application and a separate Razor Class library.
The RCL contains some common partial views and functions which I intend to use in separate projects.
I am trying to make an AJAX post to a controller which looks like this:
using Microsoft.AspNetCore.Mvc;
namespace ThinkkaGlobal
{
[ApiController]
public class LoginController : Controller
{
[HttpPost("thinkka/login")]
public ActionResult DoLogin([FromBody] ThinkkaUsers.Requests.LoginRequest Req)
{
return PartialView("Login", Req);
}
}
}
Note the Attribute Routing... this is not necessarily what I want, but I have spent hours now trying to get this to work that I have tried many, many different routing options. I have also got app.UseMvcWithDefaultRoute();
in my Startup.cs
file.
Ultimately, what I want is an AJAX post which will return a partial view... and I would like to do this using jquery-unobtrusive-ajax
, and that is how I started, but then I started using jQuery AJAX directly
so I could mess about with it a bit more.
So, very basic AJAX...
$.ajax({
url: "thinkka/login",
type: 'post',
contentType: "application/json", // (application/x-www-form-urlencoded; charset=UTF-8)
dataType: 'json',
success: function (data) {
console.log(data);
},
data: { email: "jamie@jamiehartnoll.co.uk" }
});
Just a quick note, the POSTed Object is a simple Object as follows:
class LoginRequest
{
public string email { get; set; }
public string password { get; set; }
public bool remember { get; set; }
public int location { get; set; }
public string userpass { get; set; }
}
When I POST using contentType application/x-www-form-urlencoded; charset=UTF-8
I get Error 415 - Unsupported Media Type
When I POST using contentType application.json
I get Error 404 - Not Found
For hours I was just getting 404, but I think I was genuinely using the wrong routing then. I was using thinkka/login/DoLogin
as the URL, ie, including the called method name, but the routing attribute overrides this, so in all that time I wasted trying to get that to work, I think I was just using the wrong URL!
However... now... the fact that I can at least get a different error code does suggest the URL is correct?
But I don't know why I still get 404 which I used application/json
, although in fact, application/x-www-form-urlencoded; charset=UTF-8
would be the correct contentType.... wouldn't it....
I don't know... it's late, I've been doing this for hours and it makes no sense anymore!
As always any help would be very much appreciated!
Well... I solved the problem in two stages...
Firstly to correctly return a Partial
which is what I ultimately want, I changed the Controller class as follows
using Microsoft.AspNetCore.Mvc;
namespace ThinkkaGlobal
{
[ApiController]
public class LoginController : Controller
{
[Route("thinkka/login")]
[HttpPost]
public PartialViewResult DoLogin([FromBody()] ThinkkaUsers.Requests.LoginRequest Req)
{
return PartialView("Login", Req);
}
}
}
Secondly I changed the AJAX to:
$.ajax({
url: "thinkka/login",
type: 'post',
contentType: "application/json",
dataType: 'json',
success: function (data) {
alert('Success ' + data);
},
data: JSON.stringify({ email: "Email Address" })
});
Without Stringify-ing
the JSON I had a 400
error on my updated Controller.
No changes to startup.cs
which still contains no explicit route declarations except for app.UseMvcWithDefaultRoute();
- as mentioned previously, ,the attribute routing is needed here.
Using application/x-www-form-urlencoded; charset=UTF-8
in my AJAX I get 415
which now makes sense.
So I still don't understand
Why do I have to stringify
my JSON Post Data - or more specifically what changes do I need to make so that I don't need to...
Why was I getting 404
with my previous Controller, which was the same in every regard except that it was declaring a ActionResult
type... which in fact, for my purposes was wrong anyway, but for learning purposes.... why?