我正在尝试使用C#实现WebAPI 2。在我的开发设置中一切正常,但是当我将它部署到实时服务器时,DELETE函数会导致错误。
我已将功能剥离到基础,但仍会导致错误。所有其他API调用继续正常工作。
代码是:
[HttpDelete]
public string Delete(int id)
{
return "delete";
}
错误是:
<div id="content">
<div class="content-container"><fieldset>
<h2>500 - Internal server error.</h2>
<h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</fieldset></div>
</div>
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Object reference not set to an instance of an object.
</ExceptionMessage>
<ExceptionType>System.NullReferenceException</ExceptionType>
<StackTrace>
at System.Web.Http.ApiController. <InvokeActionWithExceptionFilters>d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher. <SendAsync>d__0.MoveNext()
我的web.config如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<connectionStrings>
<add name="PlaneLoggerEntities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=System.Data.SqlClient;provider connection string="data source=.\MSSQLSERVER2012;initial catalog=PlaneLogger_Dev;integrated security=True;pooling=False;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="PlaneRegistrationsEntities" connectionString="metadata=res://*/Models.Aircraft.csdl|res://*/Models.Aircraft.ssdl|res://*/Models.Aircraft.msl;provider=System.Data.SqlClient;provider connection string="data source=.\MSSQLSERVER2012;initial catalog=PlaneRegistrations;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<add name="BasicAuthHttpModule" type="PlaneLogger.API.Modules.BasicAuthHttpModule" />
<remove name="WebDAVModule"/>
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<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>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
请帮助,现在正在做我的坚果!
答案 0 :(得分:0)
交叉检查如下: 你真的把int值作为id传递。 2.删除方法的控制器是否启用了Cors策略。 3.您是否在正文中发送任何数据对象,如果是,则使用相应的类检查其正确性。 4.使用([FromRoute] int Id)作为参数。
我希望它对你有用。
答案 1 :(得分:0)
经过一番挖掘,我找到了答案。
基本上,问题是由于IIS如何处理模块和请求处理程序。默认情况下,WebDAV将配置为侦听PUT和DELETE请求。通常,WebDAV将在您的服务器上禁用,但是这不会删除绑定,任何PUT和DELETE请求仍将传递给WebDAV,WebDAV会因为它被禁用而拒绝它们。
您需要通过Web配置删除WebDAV,如下所示:
<modules>
<remove name="WebDAVModule"/>
</modules>
<handlers>
<remove name="WebDAV" />
</handlers>
您需要这两个条目才能正常工作。我的web.config中只有删除模块条目。