Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FormUrlEncodedContent与列表对象和POST方法的其他混合数据类型_C#_.net_Web Services_Asp.net Web Api_Http Post - Fatal编程技术网

C# FormUrlEncodedContent与列表对象和POST方法的其他混合数据类型

C# FormUrlEncodedContent与列表对象和POST方法的其他混合数据类型,c#,.net,web-services,asp.net-web-api,http-post,C#,.net,Web Services,Asp.net Web Api,Http Post,POST方法 SaveData([FromBody]MyDetails myDetails) MyDetails是一个具有 public int Id; public int LocationId; public List<Employee> Employee; public bool Status; public int-Id; 公共地址ID; 公开名单雇员; 公共布尔状态; Employee是一个具有 public int EmployeeId; public Name E

POST方法

SaveData([FromBody]MyDetails myDetails)
MyDetails是一个具有

public int Id;
public int LocationId;
public List<Employee> Employee;
public bool Status;
public int-Id;
公共地址ID;
公开名单雇员;
公共布尔状态;
Employee是一个具有

public int EmployeeId;
public Name EmployeeName;



var values = new Dictionary<string, string>
                        {
                            {"Id",myDetails.Id.ToString()},
                            {"LocationId", myDetails.LocationId.ToString()},
                            {"Status", myDetails.Status.ToString()},
                 {"Employee", myDetails.Employee.ToString()} -- How do i send List Employee part of FormURLEncodedContent, i know this is wrong, i am having hard time getting this to work?

                        };

var encodedContent = new FormUrlEncodedContent(values);
 var response = await client.PostAsync(url, encodedContent); //url points to POST method SaveData, which accepts MyDetails class object as parameter.
public intemployeeid;
公共名称EmployeeName;
var值=新字典
{
{“Id”,myDetails.Id.ToString()},
{“LocationId”,myDetails.LocationId.ToString()},
{“Status”,myDetails.Status.ToString()},
{“Employee”,myDetails.Employee.ToString()}--如何发送FormURLEncodedContent中的List Employee部分,我知道这是错误的,我很难让它正常工作?
};
var encodedContent=新表单URLEncodedcontent(值);
var response=wait client.PostAsync(url,encodedContent)//url指向POST方法SaveData,该方法接受MyDetails类对象作为参数。

序列化objecto JSON类

var content = JsonConvert.SerializeObject(myDetails); //myDetails is my class object.
var buffer = System.Text.Encoding.UTF8.GetBytes(content);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");   

 var response = await client.PostAsync(url, byteContent);
产生了正确的结果。

我就是这样做的。 这是我的课堂资料

 public class AddTenantRequestdto
{
    public IFormFile TenantLogo { get; set; }    
    public string TenantTitle { get; set; } 
    public List<string> ApplicationName { get; set; }  
    public bool EnableOTP { get; set; }

}
扩展方法在这里,添加Newtonsoft.Json包:-

 public static class ExtensionMethods
{
    public static IDictionary<string, string> ToKeyValue(this object metaToken)
    {
        if (metaToken == null)
        {
            return null;
        }

        // Added by me: avoid cyclic references
        var serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
        var token = metaToken as JToken;
        if (token == null)
        {
            // Modified by me: use serializer defined above
            return ToKeyValue(JObject.FromObject(metaToken, serializer));
        }

        if (token.HasValues)
        {
            var contentData = new Dictionary<string, string>();
            foreach (var child in token.Children().ToList())
            {
                var childContent = child.ToKeyValue();
                if (childContent != null)
                {
                    contentData = contentData.Concat(childContent)
                                             .ToDictionary(k => k.Key, v => v.Value);
                }
            }

            return contentData;
        }

        var jValue = token as JValue;
        if (jValue?.Value == null)
        {
            return null;
        }

        var value = jValue?.Type == JTokenType.Date ?
                        jValue?.ToString("o", CultureInfo.InvariantCulture) :
                        jValue?.ToString(CultureInfo.InvariantCulture);

        return new Dictionary<string, string> { { token.Path, value } };
    }

    public static FormUrlEncodedContent ToFormData(this object obj)
    {
        var formData = obj.ToKeyValue();

        return new FormUrlEncodedContent(formData);
    }
}
公共静态类扩展方法
{
公共静态IDictionary ToKeyValue(此对象元令牌)
{
if(metaToken==null)
{
返回null;
}
//由我添加:避免循环引用
var serializer=newjsonserializer{ReferenceLoopHandling=ReferenceLoopHandling.Ignore};
var-token=作为JToken的元令牌;
if(标记==null)
{
//由我修改:使用上面定义的序列化程序
返回ToKeyValue(JObject.FromObject(metaToken,serializer));
}
if(token.HasValues)
{
var contentData=new Dictionary();
foreach(token.Children().ToList()中的var child)
{
var childContent=child.ToKeyValue();
if(childContent!=null)
{
contentData=contentData.Concat(childContent)
.ToDictionary(k=>k.Key,v=>v.Value);
}
}
返回内容数据;
}
var jValue=标记为jValue;
if(jValue?.Value==null)
{
返回null;
}
var value=jValue?.Type==JTokenType.Date?
jValue?.ToString(“o”,CultureInfo.InvariantCulture):
jValue?.ToString(CultureInfo.InvariantCulture);
返回新字典{{token.Path,value}};
}
公共静态FormUrlEncodedContent ToFormData(此对象对象对象)
{
var formData=obj.ToKeyValue();
返回新的FormUrlEncodedContent(formData);
}
}
希望我解释得很好,对我来说很好

 public static class ExtensionMethods
{
    public static IDictionary<string, string> ToKeyValue(this object metaToken)
    {
        if (metaToken == null)
        {
            return null;
        }

        // Added by me: avoid cyclic references
        var serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
        var token = metaToken as JToken;
        if (token == null)
        {
            // Modified by me: use serializer defined above
            return ToKeyValue(JObject.FromObject(metaToken, serializer));
        }

        if (token.HasValues)
        {
            var contentData = new Dictionary<string, string>();
            foreach (var child in token.Children().ToList())
            {
                var childContent = child.ToKeyValue();
                if (childContent != null)
                {
                    contentData = contentData.Concat(childContent)
                                             .ToDictionary(k => k.Key, v => v.Value);
                }
            }

            return contentData;
        }

        var jValue = token as JValue;
        if (jValue?.Value == null)
        {
            return null;
        }

        var value = jValue?.Type == JTokenType.Date ?
                        jValue?.ToString("o", CultureInfo.InvariantCulture) :
                        jValue?.ToString(CultureInfo.InvariantCulture);

        return new Dictionary<string, string> { { token.Path, value } };
    }

    public static FormUrlEncodedContent ToFormData(this object obj)
    {
        var formData = obj.ToKeyValue();

        return new FormUrlEncodedContent(formData);
    }
}