2016-07-12 13 views
0

私はC#を使用してWebAPI 2を実装しようとしています。私の開発環境では問題なく動作していますが、ライブサーバーにデプロイすると、DELETE機能がエラーを起こしています。WebAPI削除原因500 /オブジェクトが見つかりません

私は基本機能を削除しましたが、それでもエラーが発生します。他のすべての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=&quot;data source=.\MSSQLSERVER2012;initial catalog=PlaneLogger_Dev;integrated security=True;pooling=False;multipleactiveresultsets=True;App=EntityFramework&quot;" 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=&quot;data source=.\MSSQLSERVER2012;initial catalog=PlaneRegistrations;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 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

次に、問題はあなたのコードではないようです(devで動作するので)、デプロイ/ライブサーバーを制御できますか?あなたがトラブルシューティングする必要があると思います – Jocke

+0

はい、私はそうですが、どこから始めたらいいかわかりません!最初は「削除できません」というメッセージが表示されていましたが、「」を追加するとそのことが解決されました。私は何かとても単純な理由が私に500を与える理由を見ることができません - GET/POST/PUTはすべて正常に動作します –

+0

どのようにホストされていますか? IIS? – Jocke

答えて

0

いくつかの掘削の後、私は答えを見つけることができました。

基本的に、この問題は、IISがモジュールとハンドラをどのように処理するかに起因していました。デフォルトでは、WebDAVはPUTおよびDELETE要求を待機するように構成されます。通常、WebDAVはサーバー上で無効になりますが、バインディングは削除されません.PUTおよびDELETE要求はWebDAVに引き渡され、無効になっているためWebDAVが拒否されます。

次のように、Web構成を経由してWebDAVを削除する必要があります。

<modules> 
    <remove name="WebDAVModule"/> 
</modules> 
<handlers> 
    <remove name="WebDAV" /> 
</handlers> 

それを動作させるためにあなたがこれらのエントリの両方が必要です。私はweb.configにremoveモジュールのエントリしかありませんでした。

0

次のようなクロスチェックは、 です。1.実際にはint値をidとして渡していますか? 2. DeleteメソッドのコントローラでCorsポリシーが有効になっていますか。 3.本体にデータオブジェクトを送信していますか?はいの場合は、対応するクラスで正しいかどうかを確認してください。 4.パラメータとして([FromRoute] int Id)を使用します。

私はそれがあなたのために働くことを望みます。