Web Api<;T>;方法

Web Api<;T>;方法,api,model-view-controller,web,Api,Model View Controller,Web,我的问题很有趣。我的代码 public JsonResult<ResultStatus> Post<T>(string query, [FromBody]T value) where T: GelYikaBase { return Json(new ResultStatus()); } public JsonResult Post(字符串查询,[FromBody]T值),其中T:GelYikaBase { 返回Json(newResul

我的问题很有趣。我的代码

public JsonResult<ResultStatus> Post<T>(string query, [FromBody]T value) where T: GelYikaBase
    {

        return Json(new ResultStatus());
    }
public JsonResult Post(字符串查询,[FromBody]T值),其中T:GelYikaBase
{
返回Json(newResultStatus());
}
我想用我来做,这是保存数据库中任何对象的唯一方法。但我不知道如何以这种方式插入URL函数

错误

{"Message": "An error has occurred." 
"ExceptionMessage": "Cannot call action method 'System.Web.Http.Results.JsonResult`1[GelYikaFreamwork.Data.ResultStatus] Post[T](System.String, T)' on controller 'Gel_Yıka_Servis.Controllers.ServisController' because the action method is a generic method."
"ExceptionType": "System.InvalidOperationException"
"StackTrace": " konum: System.Web.Http.Controllers.ReflectedHttpActionDescriptor.InitializeActionExecutor(MethodInfo methodInfo) konum: System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<InitializeProperties>b__4() konum: System.Lazy`1.CreateValue() konum: System.Lazy`1.LazyInitValue() konum: System.Lazy`1.get_Value() konum: System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- Özel durumun oluşturulduğu önceki konumdan başlayan yığın izlemesinin sonu --- konum: System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) konum: System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) konum: System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() konum: System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- Özel durumun oluşturulduğu önceki konumdan başlayan yığın izlemesinin sonu --- konum: System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) konum: System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) konum: System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() konum: System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- Özel durumun oluşturulduğu önceki konumdan başlayan yığın izlemesinin sonu --- konum: System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) konum: System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) konum: System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() konum: System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
{“消息”:“发生错误。”
“ExceptionMessage:“无法调用操作方法”System.Web.Http.Results.JsonResult`1[GelYikaFreamwork.Data.ResultStatus]在控制器“Gel_Yıka_Servis.Controllers.ServisController”上发布[T](System.String,T),因为操作方法是通用方法。”
“异常类型”:“System.InvalidOperationException”
“StackTrace:“konum:System.Web.Http.Controllers.ReflectedHttpActionDescriptor.InitializeActionExecutor(MethodInfo MethodInfo)konum:System.Web.Http.Controllers.ReflectedHttpActionDescriptor.b_u4()konum:System.Lazy`1.CreateValue()konum:System.LazyInitValue()konum:System.LazyintValue`1.get_Value()konum:System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2参数,CancellationToken CancellationToken)----Özel durumun oluşturulduğuönceki konumdan başlayan yığn izlemesinin sonu--konum:System.Runtime.CompilerServices.Taskwaiter.ThrowforOnSuccess(任务任务)konum:System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)konum:System.Runtime.CompilerServices.TaskWaiter`1.GetResult()konum:System.Web.Http.Controller.ApiControllerActionInvoker.d_0.MoveNext()---奥卢图鲁尔德乌恩斯基·科努姆丹·巴伊拉安·伊兹勒梅西宁·索努---konum:System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务任务)konum:System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务)konum:System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()konum:System.Web.Http.Controllers.ActionFilterResult.dèu 2.MoveNext()--Özel durumun oluşturulduğuönceki konumdan başlayan yığn izlemesinin sonu--konum:System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)konum:System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)konum:System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()konum:System.Web.Http.Dispatcher.HttpControllerDispatcher.d_u1.MoveNext()
}

这种方法的问题是Web API不知道如何反序列化对象:

泛型的常见用法如下:

// My generic method
void GenericMethod<T>(){}
... 
// Call the generic method by specifying the type of T:
GenericMethod<string>()
您可以编写一个泛型控制器来处理从
GelYikaBase
继承的类:

public class GelYikaBaseController<T> : ApiController where T : GelYikaBase
{
    public JsonResult<ResultStatus> Post(string query, T value)
    {
        return Json(new ResultStatus());
    }
}

public class GelYika1Controller<T> : GelYikaBaseController<GelYika1> { }

public class GelYika2Controller<T> : GelYikaBaseController<GelYika2> { }
公共类GelYikaBase控制器:ApiController其中T:GelYikaBase
{
公共JsonResult Post(字符串查询,T值)
{
返回Json(newResultStatus());
}
}
公共类Gelyika1控制器:GelYikaBaseController{}
公共类GelYika2Controller:GelYikaBaseController{}

这样,Web Api知道如何反序列化
GelYikaBase
对象。

这种方法的问题是Web Api不知道如何反序列化对象:

泛型的常见用法如下:

// My generic method
void GenericMethod<T>(){}
... 
// Call the generic method by specifying the type of T:
GenericMethod<string>()
您可以编写一个泛型控制器来处理从
GelYikaBase
继承的类:

public class GelYikaBaseController<T> : ApiController where T : GelYikaBase
{
    public JsonResult<ResultStatus> Post(string query, T value)
    {
        return Json(new ResultStatus());
    }
}

public class GelYika1Controller<T> : GelYikaBaseController<GelYika1> { }

public class GelYika2Controller<T> : GelYikaBaseController<GelYika2> { }
公共类GelYikaBase控制器:ApiController其中T:GelYikaBase
{
公共JsonResult Post(字符串查询,T值)
{
返回Json(newResultStatus());
}
}
公共类Gelyika1控制器:GelYikaBaseController{}
公共类GelYika2Controller:GelYikaBaseController{}

这样做,Web Api知道如何反序列化
GelYikaBase
对象。

问题是您的控制器不知道要绑定哪个模型。您应该看看reflectedhttpdescriptor类。可能是您可以覆盖默认行为以绑定到您的模型。我添加了一个答案问题是您的控制器不知道了解要绑定的模型。您应该看看reflectedhttpdescriptor类。可能您可以覆盖默认行为以绑定到您的模型。我已经添加了一个答案