I have a WPF front end calling an API. Everything was fine in Framework 4.72 but when the API was upgraded to netCore 3.1 the DataTables part stopped working. The part with a TranslationModel continues to work correctly. I assume its something to do with netCore not supporting DataTables natively, but I cannot find an easy solution. The solutions I've seen
use Ajax to send something to the API which I'm not.
WPF Side - I'm not using Ajax. I have a WPF application accessing the Web API.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Net.Http;
using System.Threading.Tasks;
public async Task<int> CreateTranslation(TranslationModel Translation)
{
using (HttpResponseMessage response = await _apiHelper.ApiClient.PostAsJsonAsync("/api/Translation", Translation).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
int result = await response.Content.ReadAsAsync<int>().ConfigureAwait(false);
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
public async Task CreateTranslations(DataTable translations)
{
using (HttpResponseMessage response = await _apiHelper.ApiClient.PostAsJsonAsync("/api/Translation/Translations", translations).ConfigureAwait(false))
{
if (response.IsSuccessStatusCode)
{
// Notify success
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
API Side - .NET Framework 4.72
using System.Collections.Generic;
using System.Data;
using System.Web.Http;
=> DataTables => Working
API Side - .Net Core 3.1
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
=> DataTables => Bad request under netCore3.1
[HttpPost]
public int Post([FromBody] TranslationModel model) // Works correctly under netCore3.1
{
return _translationData.CreateTranslation(model);
}
[HttpPost, Route("Translations")]
public void Post([FromBody] DataTable dataTable) // Bad Request under netCore3.1
{
_translationData.CreateTranslations(dataTable);
}
When I change "[FromBody] DataTable dataTable" to "object value", it gets matched and the value object I get is a {Newtonsoft.Json.Linq.JObject} (edited for brevity) with 3 ChildTokens of:
{[DataTable.RemotingVersion, {2.0}]}
{[XmlSchema, {<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Translation">
<xs:complexType>
<xs:sequence>
<xs:element name="DictionaryId" type="xs:int" msdata:targetNamespace="" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="tmpDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Translation" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded" />
</xs:complexType>
</xs:element>
</xs:schema>}]}
{[XmlDiffGram, {<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<tmpDataSet>
<Translation diffgr:id="Translation1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<DictionaryId>24002</DictionaryId> ... the rest of the data
So I see my datatable with all the information in the object, but how do I get it into a DataTable? Framework 472 did this automatically. Is there an extension I'm missing to let netCore pick up DataTable easily like Framework 472?
Thanks
question from:
https://stackoverflow.com/questions/65873827/datatables-not-recognized-in-server-side-net-core-3-1 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…