Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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# MVC4.net发送通用对象(不一定是json)_C#_Json_Asp.net Mvc 4 - Fatal编程技术网

C# MVC4.net发送通用对象(不一定是json)

C# MVC4.net发送通用对象(不一定是json),c#,json,asp.net-mvc-4,C#,Json,Asp.net Mvc 4,我有一个MVC4应用程序。 在一个post操作中,我希望有一个类型为“object”的参数。 它应该能够从客户端接受一个数字、一个字符串以及通用json 我尝试使用以下模型绑定器实现它: public class ObjectModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingC

我有一个MVC4应用程序。 在一个post操作中,我希望有一个类型为“object”的参数。 它应该能够从客户端接受一个数字、一个字符串以及通用json

我尝试使用以下模型绑定器实现它:

public class ObjectModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (value == null || string.IsNullOrEmpty(value.AttemptedValue))
        {
            return null;
        }
        else
        {
            var js = new JavaScriptSerializer();
            var result = js.Deserialize<object>((string)value.ConvertTo(typeof(string)));
            return result;
        }
    }
}
公共类ObjectModelBinder:DefaultModelBinder
{
公共重写对象BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)
{
var value=bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if(value==null | | string.IsNullOrEmpty(value.AttemptedValue))
{
返回null;
}
其他的
{
var js=新的JavaScriptSerializer();
var result=js.Deserialize((string)value.ConvertTo(typeof(string));
返回结果;
}
}
}
在客户端中,我使用jquery ajax发布数据,如果值是javascript对象,我也使用JSON.stringify

当我发送json或int时,它可以工作,但如果我尝试发送字符串,它将抛出一个异常——“无效的json原语:字符串值”

我应该用别的东西吗

感谢您的帮助。

问题在于JSON是一个字符串,因此您需要能够区分JSON字符串和非JSON字符串。请尝试以下方法:

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (value == null || string.IsNullOrEmpty(value.AttemptedValue))
        {
            return null;
        }
        else
        {
            int n;
            string s = (string)value.ConvertTo(typeof(string));
            if (s.StartsWith("{") || s.StartsWith("["))
            {
                var js = new JavaScriptSerializer();
                var result = js.Deserialize<object>(s);
                return result;
            }
            else if (int.TryParse(s, out n))
            {
                return n;
            }
            return s;
        }
    }
public覆盖对象BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)
{
var value=bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if(value==null | | string.IsNullOrEmpty(value.AttemptedValue))
{
返回null;
}
其他的
{
int n;
字符串s=(string)value.ConvertTo(typeof(string));
if(s.StartsWith(“{”)| s.StartsWith(“[”)
{
var js=新的JavaScriptSerializer();
var result=js.反序列化;
返回结果;
}
else if(内锥虫(s,外n))
{
返回n;
}
返回s;
}
}

您要发送的字符串是什么?