C Webapi Post Method With Enum Input Parameter Stack Overflow
C Webapi Post Method With Enum Input Parameter Stack Overflow You have a few different options to solve this: if you can change the data that is being sent to readclientmessagesc, pass the data as an int (the enum value) rather than the enum description. if you can't change the incoming data, change the parameter type to string and use enum.parse in the method body or;. By following the steps outlined in this post, you can effectively handle enum parameters in your c# web api projects. start leveraging the power of enums in your web api development today!.
C Parameter Binding In Webapi Post Method Stack Overflow When passing enums as parameters in a web api, it is best practice to use the underlying integer value of the enum instead of the enum name as a string. this is because using the enum name as a string can be error prone and can lead to issues if the enum name is changed in the future. It accepts both the numeric and string value of the enums, validates them, and handles the 400 bad request return result and error message for you. the only thing i wish it did do is provide all the valid enum values in the error message so that a developer can see what needs to change. Describes how web api binds parameters and how to customize the binding process in asp 4.x. I'm making a simple api in core and am trying to realise a post, where i need to handle an enum value. i'm using a "saveprofileresource" for that, which is beeing used by automapper in my controller class.
C Enum Returning String Value In Webapi Stack Overflow Describes how web api binds parameters and how to customize the binding process in asp 4.x. I'm making a simple api in core and am trying to realise a post, where i need to handle an enum value. i'm using a "saveprofileresource" for that, which is beeing used by automapper in my controller class. Create a new dto that will receive the data in your post method and add a custom validator that will make sure the string supplied is valid. the custom validator tries to convert the string supplied to a valid addresstype enum. You can send a string (string name of an enum) to the web api controller (even if it has a numeric value like the one in op's example). asp framework already has provisions for this conversion to happen automatically you just need to enable it. Learn how web api binds http request data to the parameters of an action method here.
Comments are closed.