【发布时间】:2018-02-16 19:46:08
【问题描述】:
我正在编写一个 Web API 来接收遥测数据。十六进制字符串形式的遥测数据应 POST 到 Web API URL。此 Web API 不需要身份验证、RoutePrefix 和 Route。内容类型:text/plain
这是我的代码。
[Route("")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class MyData_Controller : ApiController
{
[HttpPost]
[ActionName("DefaultAction")]
public Task<IHttpActionResult> DataPost(String myData)
{
SaveData(myData);
return Task.FromResult<IHttpActionResult>(Json(true));
}
protected void SaveData(String myData)
{
}
}
然后我将此 WebAPI 托管到 IIS8.5。在 web.config 中,我还添加了以下内容
<system.webServer>
<handlers accessPolicy="Read, Execute, Script">
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="WebDAV" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*."
verb="GET,HEAD,POST,PUT"
type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Execute" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
</system.webServer>
当我在 Postman 中使用 POST 对其进行测试时,出现以下错误
HTTP Error 405.0 - Method Not Allowed
The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.
Detailed Error Information:
Module DirectoryListingModule
Notification ExecuteRequestHandler
Handler StaticFile
Error Code 0x80070001
Requested URL http://MyDataServer.com:2331/
Physical Path E:\Applications\Web API\MyDataServer_WebAPI
Logon Method Anonymous
Logon User Anonymous
邮递员标题显示
Allow → GET, HEAD, OPTIONS, TRACE
是否表示动词 POST 或 PUT 未启用?
我检查了 StaticFile 处理程序,它在 Handler Mappings 和 Requested Restrictions 中进行了配置 -> Verbs 选择了所有动词。
我的实现和站点配置有什么问题?
【问题讨论】:
标签: c# asp.net-web-api asp.net-web-api-routing