Currently I'm having an issue with posting some data to web api. I have two different project. One project is a web application that is running web api 2.0. The other project is a simple console application that creates a new http client, builds up a json object, and attempts to post it to the web api controller. However, doing posts I keep getting 405 (Method not allowed...). I've scoured the internet looking for solutions. I've even gone as far as checking handlers in IIS Express and comparing that with what I have in web.config. Still no luck.
So here is what I have for my web api:
[RoutePrefix("api/media")]
[Authorize]
public class MultimediaController : ApiController
{
[HttpPost, Route("test")]
public IHttpActionResult Store([FromBody] TestRequest request) {
return Ok(request);
}
...
Next, here is my Test model:
public class TestRequest {
public string testProp1 { get; set; }
public string testProp2 { get; set; }
}
Then, here is a snip of code from my console app that attempts to do the POST:
private const string testUrl = "/api/media/test/";
private async Task<bool> RunPostTest()
{
var httpClient = CreateHttpClient();
var content = JsonConvert.SerializeObject(new TestRequest {testProp1 = "hello", testProp2 = "world"});
var jsonContent = new StringContent(content, Encoding.UTF32, "application/json");
var response = await httpClient.PostAsync(testUrl, jsonContent);
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine($"Response code: {response.StatusCode}");
return false;
}
return true;
}
Here is my web.config for handlers:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
I checked iisexpress applicationhost.config. Lots of handlers in there, but I'll just post the last few as those seem like the pertinent ones. Also note that WebDAV is all commented out everywhere...
...
<add name="TRACEVerbHandler" path="*" verb="TRACE" modules="ProtocolSupportModule" requireAccess="None" />
<add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="ProtocolSupportModule" requireAccess="None" />
<add name="ExtensionlessUrl-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
</handlers>
</system.webServer>
This constantly returns an http 405. I have other methods in here that perform GETs, and those hit the end points just fine.
Also worth noting I used an external app to perform a post, such as Postman, sending the raw json in the body. And the endpoint was hit! Seems like it has something to do with the set up of iisexpress, but I could be wrong.
Anyone have any idea what could be the case? Or refer me to some link that could uncover the problem?
response object I'm getting back from debugging using immediate window:
response
{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZ204NDBkXERvY3VtZW50c1xQcm9qZWN0c1xWaXN1YWwgU3R1ZGlvIFByb2plY3RzXEdpdFxNb2JpbGl0eSBDT0VcU0JWVFxTQlZUXFNCVlRcYXBpXG1lZGlhXHRlc3Rc?=
Cache-Control: no-cache
Date: Mon, 16 Jan 2017 17:39:32 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 1218
Allow: POST
Content-Type: text/html
Expires: -1
}}
Content: {System.Net.Http.StreamContent}
Headers: {Pragma: no-cache
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZ204NDBkXERvY3VtZW50c1xQcm9qZWN0c1xWaXN1YWwgU3R1ZGlvIFByb2plY3RzXEdpdFxNb2JpbGl0eSBDT0VcU0JWVFxTQlZUXFNCVlRcYXBpXG1lZGlhXHRlc3Rc?=
Cache-Control: no-cache
Date: Mon, 16 Jan 2017 17:39:32 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
}
IsSuccessStatusCode: false
ReasonPhrase: "Method Not Allowed"
RequestMessage: {Method: POST, RequestUri: 'http://localhost:45106/api/media/test/', Version: 1.1, Content: System.Net.Http.StringContent, Headers:
{
Accept: application/json
Content-Type: application/json; charset=utf-32
Content-Length: 164
}}
StatusCode: MethodNotAllowed
Version: {1.1}