2016-08-23 4 views
0

私は以下のアプリケーションにModelStateにErrorMessageは、Web APIで空である

public class APIVesselFilter 
    { 
     [Required(ErrorMessage = "Vessel is required")] 
     public Guid VesselID { get; set; } 

     public Guid? AnchorageID { get; set; } 

    } 

に使用ModelクラスにModelStateが有効であるとかどうかをチェックします検証フィルターされている以下の私は、エラーを送信します有効でない場合メッセージ。

public class ValidateModelAttribute : ActionFilterAttribute 
    { 
     public override void OnActionExecuting(HttpActionContext actionContext) 
     { 
      if (actionContext.ModelState.IsValid == false) 
      { 
       List<string> errorList = new List<string>(); 
       foreach (var err in actionContext.ModelState.Values) 
       { 

        foreach (var er in err.Errors) 
        { 
         errorList.Add(er.ErrorMessage); 
        } 
       } 
       actionContext.Response = actionContext.Request.CreateResponse(APIResponse.SendResponse(RequestStatus.GetValidationFailedMessage(), actionContext.ModelState)); 
      } 
     } 
    } 

ここでは、上記のモデル状態を使用した場合の応答を示します。ここでは、エラーメッセージはユーザーがわかりやすい方法ではないので、VesselのRequire属性にErrorMessageを追加して、ModelStateのエラーをループします。しかし、私のエラーメッセージは常に空です(私はこれをデバッガを使ってチェックしました)。エラーメッセージがModelStateに直接バインドされるように、私はここで何が欠けていますか?

{ 
    "Status": { 
    "StatusCode": 620, 
    "StatusMessage": "Validation Failed" 
    }, 
    "Data": { 
    "filter": { 
     "_errors": [ 
     { 
      "<Exception>k__BackingField": { 
      "ClassName": "Newtonsoft.Json.JsonSerializationException", 
      "Message": "Required property 'VesselID' not found in JSON. Path '', line 1, position 56.", 
      "Data": null, 
      "InnerException": null, 
      "HelpURL": null, 
      "StackTraceString": " at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EndObject(Object newObject, JsonReader reader, JsonObjectContract contract, Int32 initialDepth, Dictionary`2 propertiesPresence)", 
      "RemoteStackTraceString": null, 
      "RemoteStackIndex": 0, 
      "ExceptionMethod": "8\nEndObject\nNewtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed\nNewtonsoft.Json.Serialization.JsonSerializerInternalReader\nVoid EndObject(System.Object, Newtonsoft.Json.JsonReader, Newtonsoft.Json.Serialization.JsonObjectContract, Int32, System.Collections.Generic.Dictionary`2[Newtonsoft.Json.Serialization.JsonProperty,Newtonsoft.Json.Serialization.JsonSerializerInternalReader+PropertyPresence])", 
      "HResult": -2146233088, 
      "Source": "Newtonsoft.Json", 
      "WatsonBuckets": null 
      }, 
      "<ErrorMessage>k__BackingField": "" 
     } 
     ], 
     "<Value>k__BackingField": null 
    } 
    } 
} 

答えて

0

DisplayAttributeであなたのモデルプロパティを飾るお試しください:

public class APIVesselFilter 
{ 
    [Required] 
    [Display(Name="Vessel is required")] 
    public Guid VesselID { get; set; } 

    public Guid? AnchorageID { get; set; } 

} 

私は、これはHtml.ValidationSummaryを使用するときにあなたのメッセージをカスタマイズする方法を知っていると簡単なテストをしてにModelStateを検査する際に、これが立ち上がる示しました。行動。

0

CreateResponseの代わりにCreateErrorResponseを使用すると、問題が解決する場合があります。 私は賢明な問題に直面し、それで修正されました。このコードは、質問に答える方法および/または、なぜそれが答えの長期的な価値を向上させるという問題を解決に関する追加のコンテキストを提供するかもしれないが、あなたの場合は これは

actionContext.Response = actionContext.Request.CreateErrorResponse(APIResponse.SendResponse(RequestStatus.GetValidationFailedMessage(), actionContext.ModelState)); 
+0

になります。質の高い回答を提供するためには、この[how-to-answer](http://stackoverflow.com/help/how-to-answer)をお読みください。 – thewaywewere

関連する問題