简体   繁体   English

ASP.NET MVC 中的 GUID model 绑定

[英]GUID model binding in ASP.NET MVC

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.我正在尝试从我的 Javascript 代码发出获取请求,并且 URL 包含一个 Guid。 The ASP.NET controller does not get hit/register the request to the API. ASP.NET controller 未命中/注册对 API 的请求。

I've tried a couple different things already but this is my Javascript and Controller as is:我已经尝试了一些不同的东西,但这是我的 Javascript 和 Controller 原样:

        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:我不断收到此错误:

在此处输入图像描述

I'm not sure how to properly bind the Guid such that it is received by the ASP.NET Controller.我不确定如何正确绑定 Guid,以便 ASP.NET Controller 接收它。

Any ideas??有任何想法吗?? Much appreciated and have a great day!非常感谢,祝您有美好的一天!

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:和你的 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:但是如果你使用非常旧的 MVC 并且属性路由对你不起作用,试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM