0
/api/WebService?param1=1&price=6.2 working
/api/WebService?param1=1&price=6,2 not working. The request is invalid error.

Server Region Einstellungen nach Komma und ich setzte Globalisierung „tr-TR“ auf webconfig es noch nicht mit Komma funktioniert. Außerdem habe ich ModelBinder ausprobiert aber es funktioniert auch nicht.Handhabung dezimal Parameter in WebAPI

Wie kann ich es mit Komma arbeiten lassen?

+0

Sie müssen Preis Parameter als String-Typ machen. Vielleicht ist es jetzt dezimal –

+0

ja das ist eine der Lösungen, aber das wollte ich leider nicht. –

Antwort

0

Verwendung Modelbinder wie folgt aus:

public class DecimalModelBinder : IModelBinder 
{ 
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
    ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); 
    ModelState modelState = new ModelState { Value = valueResult }; 
    object actualValue = null; 
    try 
    { 
     actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.CurrentCulture); 
    } 
    catch (FormatException e) 
    { 
     modelState.Errors.Add(e); 
    } 

    bindingContext.ModelState.Add(bindingContext.ModelName, modelState); 
    return actualValue; 
    } 
} 


protected void Application_Start() 
{ 
    AreaRegistration.RegisterAllAreas(); 
    RegisterGlobalFilters(GlobalFilters.Filters); 
    RegisterRoutes(RouteTable.Routes); 

    //HERE you tell the framework how to handle decimal values 
    ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder()); 

    DependencyResolver.SetResolver(new ETAutofacDependencyResolver()); 
} 

Gefunden in: ASP.NET MVC datetime culture issue when passing value back to controller

Verwandte Themen