I was hoping someone could help me sort this out. The solution to this is probably obvious but I can’t seem to figure out what I’m missing…
I’m trying to issue a get request from my Javascript code, and the URL contains a Guid. The ASP.NET controller does not get hit/register the request to the API.
I’ve tried a couple different things already but this is my Javascript and Controller as is:
JavaScript
x
8
1
function getChat( contact_id ) {
2
$.get("/contact/conversations", { contactId: contact_id })
3
.done( function(resp) {
4
let chat_data = resp.data || [];
5
loadChat( chat_data );
6
});
7
}
8
and…
JavaScript
1
8
1
[Route("contact/conversations")]
2
public JsonResult ConversationWithContact(Guid? contactId)
3
{
4
5
//this doesn't get hit
6
7
}
8
I keep getting this error:
I’m not sure how to properly bind the Guid such that it is received by the ASP.NET Controller.
Any ideas?? Much appreciated and have a great day!
Advertisement
Answer
Change your route to this:
JavaScript
1
12
12
1
[Route("~/contact/conversations/{id}")]
2
public JsonResult ConversationWithContact(string id)
3
{
4
5
if(!string.IsNullOrEmpty(id)){
6
7
var contactId= new Guid (id);
8
//this doesn't get hit
9
}
10
11
}
12
and your ajax:
JavaScript
1
8
1
function getChat( contact_id ) {
2
$.get("/contact/conversations/"+contact_id)
3
.done( function(resp) {
4
let chat_data = resp.data || [];
5
loadChat( chat_data );
6
});
7
}
8
but if you use very old MVC and attribute routing doesnt work for you, try this:
JavaScript
1
9
1
function getChat( contact_id ) {
2
$.get("/contact/ConversationWithContact/"+contact_id)
3
.done( function(resp) {
4
let chat_data = resp.data || [];
5
loadChat( chat_data );
6
});
7
}
8
9