2

我一直在尝试让我的 ASP.net WebAPI(ASP.net Web 角色)中的 DELETE 请求在 Azure 和我的机器上本地工作。但最终得到“未找到”错误。

我对 ASP.net WebAPI Web 角色的 web.config 有以下配置:

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <modules>
    <remove name="WebDAVModule" />
  </modules>
  <handlers>
   <remove name="WebDAV" />
   <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
   <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
   <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
   <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit"
     path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
     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,PUT,DELETE,PATCH,OPTIONS"
     modules="IsapiModule"
     scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll"
     preCondition="classicMode,runtimeVersionv4.0,bitness64"
     responseBufferLimit="0" />
   <add name="ExtensionlessUrlHandler-Integrated-4.0"
     path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />
 </handlers>
</system.webServer>

为了让它在 IISExpress 上运行,我更改了 $:\Users\\Documents\IISExpress\config\applicationhost.config 配置并注释掉其中的 WebDav 部分并适当地添加了“PUT、DELETE”动词。

为了测试我的 WebAPI,我创建了一个简单的 windows 窗体 (.net 4.5) c# 应用程序并使用了 httpclient,如下所示:

const string key = "user1";
var client = new HttpClient { BaseAddress = new Uri("http://abcd.cloudapp.net/") };
client.DefaultRequestHeaders.Accept.Add(
    new MediaTypeWithQualityHeaderValue("application/json"));
var requestUrl = string.Format("api/user/{0}", key);
var deleteAsync = client.DeleteAsync(requestUrl).Result;

我得到的回应如下:

{StatusCode: 404,
  ReasonPhrase: 'Not Found',
  Version: 1.1,
  Content: System.Net.Http.StreamContent,
  Headers:
  {
    Pragma: no-cache
    Cache-Control: no-cache
    Date: Fri, 12 Apr 2013 06:40:31 GMT
    Server: Microsoft-IIS/8.0
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Content-Length: 128
    Content-Type: application/json; charset=utf-8
    Expires: -1
}}  System.Net.Http.HttpResponseMessage

如何让它在 Azure 上运行?也可能在我的本地机器上。

4

0 回答 0