Pass empty string in Postman - asp.net-web-api

I am trying to build a web API by VS.net and test it use Postman, I would like to pass an empty string through Postman, but it is a null value instead of empty string at .net side, any idea? thanks.
Postman
.Net Web API

[DisplayFormat(ConvertEmptyStringToNull = false, NullDisplayText = "empty")]
https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.displayformatattribute.convertemptystringtonull?view=net-5.0
can you try adding this :
You can also set it globally
https://stackoverflow.com/a/60749297/6793637

Related

Cannot POST api call from .net core MVC

I have the following code in my .net core MVC application:
[HttpPost]
public void GetCustomerInfo([FromBody] string value)
{
string data = value;
}
after deploy this to my azure we app and call 'azuresiteurl/api/controller/GetCustomerInfo' page not found error occurs.
Can anyone please help?
you are using a get request but you had defined it as a post request change it as [httpget]
You should pass the string value in the request body and since it is marked as a HttpPost method, it has to be a Post call from the client.
Additionally, check if you are indeed sending the value in the request body. The content type should be coming in correctly for FromBody to work and map the primitive type.
I suggest you go through the answer of this question.
WebApi POST works without [FromBody]?

Web API 2 OData = $format not working: the request is always ignored

I have a Web API OData project and everything is working great. I'm now trying to return xml instead of JSON using the $format parameter, as opposed to specifying a header request, and it is not working. I've tried these approaches:
http://localhost:3845/api/Customer?$format=application/xml
http://localhost:3845/api/Customer?$format=xml
http://localhost:3845/api/Customer?$format=application/xml;odata.metadata=full
All without success. This article says that it is possible: https://blogs.msdn.microsoft.com/webdev/2014/03/13/getting-started-with-asp-net-web-api-2-2-for-odata-v4-0/
I have updated all of my NuGet packages, but it seems that the request is always ignored, and instead I get JSON every time.
Since the ATOM format (XML) is only a technical committee specification instead of an OASIS standard for the OData V4 protocol, the ATOM format is disabled in the ODataLib from the version 6.3.0.
The correct way to ask the OData V4 service to respond in XML is as follows:
GET http://localhost:3845/api/Customer?$format=application/atom+xml
or set the header Accept to application/atom+xml. But due to the reason mentioned above, it doesn't work for Web API OData V4.
To support $format=xml and $format=json, add the following configuration:
config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");

Parameter format issue with ASP.NET Web API controller

I am watching this pluralsight demo video on Web Api, and he is using fiddler to pass in a parameter using Http Get with the syntax of controller/parameter
So he's using http://localhost:2405/api/values/5
5 Is the parameter he's passing in.
In my code, I have everything set up exactly the same way he does... with a routing template of {controller}/{id} and a controller method with a signature of
public string Get(string zipcode)
I can pass a parameter just fine with http://localhost:2405/api/values?zipcode=25252 but if I try passing in a paramter the way he does, like http://localhost:2405/api/values/25252 I get an error saying I do not have an action available to handle that request on the controller.
What is he doing right, that I'm doing wrong?
You need to change your routing template to {controller}/{zipcode} as the name of the parameter must match the name in the template.

Windows Phone 7.1 WebClient POST without body

I am new to Web service calls; I can do simple GET and parse the JSON output. Not good with POST and especially if the URI has spaces and quotes.
I am trying to send the following URI to the server in Windows Phone 7.1 (Emulator). It says "Not found error". This error seems to be generic; it doesn't say what's really was wrong anyway.
https://aaabbb.com//services/v4/put/users/xxxxx/device?deviceId=2NDJDRkI5MEVFME -H 'Access: Token token=CXJTY'
There is no JSON body/data for this. It just this URL with the shown params. thats where data is too.
how do I do this with WebClient class or RestSharp? I tried this using WebClient class in WP 7. didn't succeed. So downloaded RestSharp; but not sure how the above URI without any JSON data could be sent.
I followed some of the posts in here to use the WeClient class. It didn't work.
You need to URL-encode your string. For that, you can do something like this:
string deviceId = HttpUtility.UrlEncode("2NDJDRkI5MEVFME -H 'Access: Token token=CXJTY'");
Uri uri = new Uri("https://aaabbb.com/services/v4/put/users/xxxxx/device");
string data = "deviceId=" + deviceID;
WebClient wc = new WebClient();
wc.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded";
wc.UploadStringAsync(uri, data);
wc.UploadStringCompleted += wc_UploadComplete;

Web API get route values

Is there any way to statically get route values from a service method (outside of a controller) that is running in a Web API context? For example, I can do the following in ASP.NET MVC:
var mvcHandler = HttpContext.Current.Handler as MvcHandler;
var routeValues = mvcHandler.RequestContext.RouteData.Values;
I'd like to find the equivalent version of this code for Web API.
When I try to debug a sample Web API request and look at HttpContext.Current.Handler it is of type HttpControllerHandler, but this type doesn't have any properties to access route data.
EDIT
To try to help provide some more information. The code I am trying to read the value from is inside of a factory class I have that builds a custom object for my application.
You can use GetRouteData() extension on HttpRequestMessage. You would need to include System.Net.Http namespace to get this.
System.Web.Http.Routing.IHttpRouteData routeData = Request.GetRouteData();
I was able to find a solution that would get the route values for either an MVC request or a Web API request.
HttpContext.Current.Request.RequestContext.RouteData

Resources