asp.net-web-api


Post data in asp.net web API with header


Hi every one I am new to Asp.net Web API and I had my question post data using asp.net Web API and got my answer accepted.
This is an extension to the same question I want to post data with some header value in the Postman and my code is as follows
public HttpResponseMessage PostCustomer([FromBody] NewUser userData, string devideId)
{
//My code
return response;
}
When I hit this in Postman passing values in JSON format in BODY - raw I got message as follows
No HTTP resource was found that matches the request URI
No action was found on the controller that matches the request.
Please help me.
It looks like you have added some additional devideId string parameter to your action. Make sure that you are supplying a value to it as a query string when making the request:
POST http://localhost:58626/api/customers?devideId=foo_bar
If you don't want to make this parameter required then you should make it optional (in terms of optional method parameter in .NET):
public HttpResponseMessage PostCustomer([FromBody] NewUser userData, string devideId = null)
{
...
}
Now you can POST to http://localhost:58626/api/customers without providing a value for this parameter.
Remark: You don't need to decorate a complex object type (such as NewUser) with the [FromBody] attribute. That's the default behavior in Web API.
UPDATE: Here's how you could read a custom header:
public HttpResponseMessage PostCustomer(NewUser userData)
{
IEnumerable<string> values;
if (this.Request.Headers.TryGetValues("X-MyHeader", out values))
{
string headerValue = values.FirstOrDefault();
}
...
return response;
}

Related Links

Accepting application/xml content in webapi controller
DataTables Warning: Requested unknown parameter 'pCodigo' for row 0
Issue with file upload in Web API 2 other then html form post
Use ELMAH with Azure Mobile Servies
Owin AAD - required parameters for signature validation
how to return value from OnActionExecuting action filter
SignalR Authorize attribute not called
Elmah.axd on WebAPI 2.2 - No HTTP Resource was found
OData attribute routing for deleting relationship between m:m entities
Sitecore GetChildren empty just after child item created
JsonFormatter in WebApi is ignored
Partial Authentication after expired token
Web api routing use only RoutePrefix
Differentiate between MVC and WebAPI in ASP.NET 5 / MVC 6
How to authenticate ionic app with Asp.net WebApi
Web Api and where should I contorol Request Header data

Categories

HOME
gridview
nunit
jmx
numbers
error-handling
seo
cublas
alsa
spring-session
crystal-lang
bing-maps
android-securityexception
openbugs
3nf
dynamic-sql
soci
solrcloud
user-experience
imagemap
google-api-client
moses
google-cloud-print
android-sharedpreferences
gp
twiml
constexpr
gitlab-api
sql-server-2000
jbutton
android-gridview
pop3
parse-tree
openalpr
dm-script
amazon-ses
opensmpp
visa
preloader
berkeley-db-je
apache-spark-dataset
jboss-4.2.x
linter
check-mk
monkey
d3-force-directed
cd-burning
pytables
latin
delta
dimension
avspeechsynthesizer
clipboard.js
cda
phpwebsocket
fink
kendo-upload
coda
sapscript
viola-jones
ocra
avahi
zend-currency
primitive-types
contrast
teaspoon
with-statement
angular-carousel
functional-java
visual-studio-express
cffile
flex++
video-codecs
fb.ui
device-width
multiplexing
failing-tests
flex-mobile
webdev.webserver
jqote
clrstoredprocedure
cuteeditor
drupal-fivestar
isapi-extension
burndowncharts
pagemethods
complex-event-processing
asynchronous-wcf-call
business-model
great-circle

Resources

Encrypt Message