2017-05-25 84 views
1

我開發web應用程序到使用過程挑選公司時。內部服務器錯誤500使用網絡API HTTP PUT

的過程很簡單,我有一個銷售訂單的一些信息,和我的一些信息創建採摘列表(例如:物品,總重量和銷售訂單ID的數量)。

我得到總是這個錯誤

內部服務器錯誤

...當我試圖用PUT方法,創建一個領料單,並將其添加到數據庫中在Web Api方面。

當我刪除[HttpPost]誤差變化:

找不到方法

不允許的方法

這是我的代碼:

客戶端

public async Task<bool> AddTarefa(ListasPicking listaPickingAdd) 
{ 
    String listaparaAdicionar = listaPickingAdd.idLista + ";" + listaPickingAdd.IDordemVenda + ";" + listaPickingAdd.peso + ";" + listaPickingAdd.itens; 
    HttpResponseMessage response = await cliente.PutAsJsonAsync("api/ListasPicking/", listaparaAdicionar); 

    return response.IsSuccessStatusCode; 
} 

網絡API邊 - 控制器

//PUT: api/ListasPicking 
[ResponseType(typeof (ListasPicking))] 
[HttpPut] 
public IHttpActionResult PutLista ([FromBody] String lista) 
{ 
    if(!ModelState.IsValid) 
    { 
     return BadRequest(ModelState); 
    } 

    String[] result = lista.Split(';'); 
    ListasPicking novaLista = new ListasPicking(); 
    novaLista.idLista=result[0]; 
    novaLista.IDordemVenda = result[1]; 
    string lista_peso_converttoDouble = result[2]; 
    novaLista.peso = Convert.ToDouble(lista_peso_converttoDouble); 
    string lista_items_converttoInt = result[3]; 
    novaLista.itens = Convert.ToInt32(lista_items_converttoInt); 

    primContext.ListasPickingGet.Add(novaLista); 
    primContext.SaveChanges(); 

    return StatusCode(HttpStatusCode.Created); 
} 

我想涉及到這類問題的一些解決方案,並且同樣的錯誤(或多個)。

的Web.config

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    https://go.microsoft.com/fwlink/?LinkId=301879 
    --> 
<configuration> 
    <configSections> 
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <section name="PrimaveraRest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </sectionGroup> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <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 debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5.2" /> 
    <httpModules> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> 
    </httpModules> 
    </system.web> 
    <system.webServer> 
    <handlers> 
     <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
     <remove name="OPTIONSVerbHandler" /> 
     <remove name="TRACEVerbHandler" /> 
    <!-- <remove name="WebDAV"/> --> 

     <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 
    </handlers> 
    <validation validateIntegratedModeConfiguration="false" /> 
    <modules> 

    <!-- <remove name="WebDAVModule"/> --> 
     <remove name="ApplicationInsightsWebTracking" /> 
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> 
    </modules> 


    </system.webServer> 
    <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <dependentAssembly> 
     <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.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="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
     <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
     </dependentAssembly> 
     <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.WebPages" 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.2.3.0" newVersion="5.2.3.0" /> 
     </dependentAssembly> 
    </assemblyBinding> 
    </runtime> 
    <system.codedom> 
    <compilers> 
     <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> 
     <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> 
    </compilers> 
    </system.codedom> 
    <applicationSettings> 
    <PrimaveraRest.Properties.Settings> 
     <setting name="Company" serializeAs="String"> 
     <value>DEMO</value> 
     </setting> 
     <setting name="User" serializeAs="String"> 
     <value>adminfixe</value> 
     </setting> 
     <setting name="Password" serializeAs="String"> 
     <value>123456</value> 
     </setting> 
    </PrimaveraRest.Properties.Settings> 
    </applicationSettings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 

UPDATE

當我重建我的解決方案,我得到一些線路是這樣的:

文件\微軟的Visual Studio的 \ 2017年\社區\的MSBuild \ 15.0 \斌\ Microsoft.Common.CurrentVersion.targets(1964,5): 警告MSB3270:有項目的處理器架構 正在建造「MSIL」,並且參考「Interop.ICrmDS800」,「86」的處理器架構 之間的不匹配。這 不匹配可能會導致運行時故障。請考慮通過 配置管理器更改項目的 針對性的處理器架構,以便使其項目和引用之間的處理器架構 ,或採取與有針對性的 處理器架構相匹配的處理器架構 引用的依賴你項目

可能是什麼問題?互操作。dll是一個包含在我的項目中的文件,它使用外部程序獲取有關銷售訂單的信息。

SOLUTION

[鍵]註釋中缺少「ListaPicking」的模式,和我經過一個DateTime變量的默認值...我只是改變例如DateTime.Today,現在它的工作:)

+1

檢查result.Length之前試圖訪問它的成員。此外,如果您調試代碼並將服務器端的異常添加到問題中會更好。 –

回答

1

你可以只發送整個模型

public async Task<bool> AddTarefa(ListasPicking listaPickingAdd) { 

    var response = await cliente.PutAsJsonAsync("api/ListasPicking/", listaPickingAdd); 

    return response.IsSuccessStatusCode;   
} 

,並在行動收到同樣的方式,而不是解構和重構它。

[ResponseType(typeof (ListasPicking))] 
[HttpPut] 
public IHttpActionResult PutLista ([FromBody] ListasPicking novaLista) { 
    if(!ModelState.IsValid) { 
     return BadRequest(ModelState); 
    } 

    primContext.ListasPickingGet.Add(novaLista); 
    primContext.SaveChanges(); 

    return StatusCode(HttpStatusCode.Created); 
} 
+0

我仍然收到錯誤...請參閱我的更新! – bullprog

+0

@bullprog遵循錯誤消息中提供的明確說明 – Nkosi

+0

@bullprog檢查出https://stackoverflow.com/a/10196549/5233410 – Nkosi

2

你打電話PutAsJsonAsync,但是當你期望一個代表json的字符串時,你傳遞一個非json字符串作爲你的數據參數。

從文檔:

發送PUT請求作爲一個異步操作以指定的URI與序列化爲JSON給定值。

爲了使它工作,改用PutAsync

+0

現在告訴該字符串不能轉換爲http.content。 – bullprog

+0

@bullprog - 你應該可以在頭文件中設置'content-type',使用'application/x-www-form-urlencoded'並以'='作爲字符串值的前綴。請參閱[文檔](https://msdn.microsoft.com/en-us/library/hh138124(v = vs.118).aspx)。 – Igor