0

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:

        function getChat( contact_id ) {
            $.get("/contact/conversations", { contactId: contact_id })
                .done( function(resp) {
                       let chat_data = resp.data || [];
                       loadChat( chat_data );
                });
        }

and...

    [Route("contact/conversations")]
    public JsonResult ConversationWithContact(Guid? contactId)
    {

      ... //this doesn't get hit

    }

I keep getting this error:

enter image description here

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!

3
  • 1
    send it as string Commented Feb 17, 2021 at 17:18
  • 1
    It may be unrelated to GUID. The query parameter is optional, so you should be able to access the endpoint even without it. The contactId would be null, but it should still be hit. Commented Feb 17, 2021 at 17:26
  • Did you add routes.MapMvcAttributeRoutes(); to your RegisterRoutes? Commented Feb 17, 2021 at 18:46

1 Answer 1

1

Change your route to this:

[Route("~/contact/conversations/{id}")]
public JsonResult ConversationWithContact(string id)
    {

      if(!string.IsNullOrEmpty(id)){

   var contactId= new Guid (id);
      ... //this doesn't get hit
} 

    }

and your ajax:

function getChat( contact_id ) {
            $.get("/contact/conversations/"+contact_id)
                .done( function(resp) {
                       let chat_data = resp.data || [];
                       loadChat( chat_data );
                });
        }

but if you use very old MVC and attribute routing doesnt work for you, try this:

function getChat( contact_id ) {
            $.get("/contact/ConversationWithContact/"+contact_id)
                .done( function(resp) {
                       let chat_data = resp.data || [];
                       loadChat( chat_data );
                });
        }

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your response. Just tried that. Still not working = (
@ajavad What is not working? What is your url now?
@ajavad - I fixed the action route. Check my answer.
Tried it. Still no. ugh. This is the request URL now: localhost:44321/contact/conversations/…
What is no? Everything should be working. What are you hiding?
|

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.