Reputation: 3369
$.http reqeust and web api both on localhost but different applications.
angular js (in other asp.net application)
return $http({
method: "POST",
url: config.APIURL + 'Parts',
data: {'final':'final'},
headers: { 'Content-Type': 'application/json' }
});
web api (in separate application)
[HttpPost]
public Part Post(string final)
{
...
}
Error response:
{"Message":"The requested resource does not support http method 'POST'."}
web api 2 - already marked with [HTTPPOST] even though not need.
My reqeust and response packet are as follow:
**General**
Request URL:http://localhost/SigmaNest.WebAPI/api/Parts
Request Method:POST
Status Code:405 Method Not Allowed
Remote Address:[::1]:80
**Response Headers**
view source
Allow:GET
Cache-Control:no-cache
Content-Length:73
Content-Type:application/json; charset=utf-8
Date:Tue, 10 Jan 2017 13:05:59 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/10.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
**Request Headers**
view source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:17
Content-Type:application/json;charset=UTF-8
Host:localhost
Origin:http://localhost
Referer:http://localhost/SigmaNest.Web/app/views/index.html
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
**Request Payload**
view source
{final: "final"}
final
:
"final"
can any one please help me to resolve this 405 error.
Upvotes: 2
Views: 3718
Reputation: 30747
ASP.Net is struggling to match up your Ajax post to an appropriate controller action because there isn't one that matches what you're trying to call.
In this case you're trying to pass an object {'final':'final'}
but are accepting a string. Post(string final)
and ASP.Net cannot match this to any particular action that has POST
enabled.
You can stringify your javascript object
return $http({
method: "POST",
url: config.APIURL + 'Parts',
data: JSON.stringify({'final':'final'}), // Strinify your object
headers: { 'Content-Type': 'application/json' }
});
Or, change your server side method to receive a class to match the object you're supplying. For example:
// DTO MyObject - .Net will ModelBind your javascript object to this when you post
public class MyObject{
public string final {get;set;}
}
// change string here to your DTO MyObject
public Part Post(MyObject final){
...
}
Upvotes: 2