model view controller - How to add errors to Modelstate in Custom mediaFormatter -


i using custom media formatter read post data multipartform in webapi. handling serialization errors , validation errors in custom action filter attribute. in formatter mapping input type object imagemedia. want add serliazation errors modelstate can handle in customfilterattribute, recieve actioncontext. here code:

public class imagemediaformatter : mediatypeformatter {     public imagemediaformatter()     {         supportedmediatypes.add(new mediatypeheadervalue("image/jpeg"));         supportedmediatypes.add(new mediatypeheadervalue("image/jpg"));         supportedmediatypes.add(new mediatypeheadervalue("image/png"));         supportedmediatypes.add(new mediatypeheadervalue("image/gif"));         supportedmediatypes.add(new mediatypeheadervalue("multipart/form-data"));     }      public override bool canreadtype(type type)     {         return type == typeof(imagemedia);     }      public override bool canwritetype(type type)     {         return false;     }      public override task<object> readfromstreamasync(        type type, stream stream, httpcontent request,        iformatterlogger formattercontext)     {         return task.factory.startnew<object>(() =>         {             var streamprovider = new multipartmemorystreamprovider();             var keys = new dictionary<string, string>();              var result = request.readasmultipartasync(streamprovider).result;              //get , remove random slashes posted vaues             var categoryid =              streamprovider.contents.first(x => x.headers.contentdisposition.name.contains("usergeneratedcategoryid")).readasstringasync().result.replace("\"", string.empty);             keys.add(constants.usergeneratedcategoryid, categoryid);              var imagebuffer =                 streamprovider.contents.first(x => x.headers.contentdisposition.name.contains(constants.imagefile)).readasbytearrayasync().result;              return new imagemedia(keys, imagebuffer);         });     }            }                public class imagemedia  {     public imagemedia(dictionary<string, string> keys, byte[] imagebuffer)         : this()     {         var keyvaluepair = new keyvaluepair<string, string>();         foreach (var property in gettype().getproperties())         {             try             {                 keyvaluepair = keys.firstordefault(pair => pair.key.tolower() == property.name.tolower());                 if (!string.isnullorempty(keyvaluepair.key))                 {                     property.setvalue(this, keyvaluepair.value, null);                 }             }             catch (exception ex)             {                 // add these serialization errors model state can handle these in custom validation action attribute                  errors.add(keyvaluepair.key, new list<string> { ex.message });                                     isvalid = false;                                 }         }         buffer = imagebuffer;     }      private imagemedia()     {         errors = new dictionary<string, list<string>>();                }      public int usergeneratedcategoryid { get; set; }     public byte[] buffer { get;set;} } 

you added errors supplied iformatterlogger context passed in readfromstreamasync method:

public override task<object> readfromstreamasync(type type, stream stream, httpcontent request,    iformatterlogger formattercontext) 

example:

formatterlogger.logerror(errorpath: "ordereddate", errormessage: "not valid date"); 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -