2016-04-04 26 views
1

Sorunum çok ilginç. Benim kodum;Web Api <T> Yöntem

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

     return Json(new ResultStatus()); 
    } 

Benimle çalışmak istiyorum, bu veritabanının herhangi bir nesnesini kaydetmenin tek yöntemi. Ancak, URL'lerin bir işlevini bu şekilde nasıl ekleyeceğimi bilmiyorum.

Hata; Senin durumunda

// My generic method 
void GenericMethod<T>(){} 
... 
// Call the generic method by specifying the type of T: 
GenericMethod<string>() 

:

jenerik yaygın bir kullanım şöyle olabilir:

{"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()" 
} 
+0

problemi, kontrol cihazınızın bağlamak için hangi modeli bilmediğidir. reflectedhttpdescriptor sınıfına bir göz atmalısınız. Belki de modellerinize bağlamak için varsayılan behavipr'ı geçersiz kılabilirsiniz. – Thomas

+0

Nasıl yapmalıyım? –

+0

Cevabınız – Thomas

cevap

0

Bu yaklaşımdaki sorun Web API nesne serisini bilmiyor olmasıdır , bir Http isteğinden Post Method'u çağırırsınız, böylece isteği çağırırken T türünü sağlamazsınız. Genel bir yönteme sahip olmak yerine, genel bir denetleyiciye sahip olmanız gerekir.

public abstract class GelYikaBase { } 

public class GelYika1 : GelYikaBase{} 

public class GelYika2 : GelYikaBase { } 

Eğer GelYikaBase devraldığı sınıfları ile başa çıkmak için genel bir kontrolör yazabilirsiniz: Diyelim ki bu modeller tanımlanmış var diyelim

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> { } 

bu yaparak, Web Api GelYikaBase serisini bilir nesne.

+0

cevabını ekledim, teşekkürler, iyi günler! :) –