我是C#的超级新手,我不知道自己在做什么.我得到了一个任务,将C#后端与本地SQL服务器一起使用,并将数据获取/发布到数据库中.
我一直在学习教程,直到我try 从SQL表中获取数据时遇到问题:
System.NotSupportdException:不支持序列化和反序列化""System.Type""实例.路径:$.Columns.DataType.
->;System.NotSupportdException:不支持序列化和反序列化""System.Type""实例."
在System.Text.Json.Serialization.Converters.UnsupportedTypeConverter1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonConverter
1.TryWrite(Utf8JsonWriter编写器、T&A;值、JsonSerializerOptions、WriteStack&A;状态)
在System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter
1.OnTryWrite(Utf8JsonWriter编写器、T值、JsonSerializerOptions、WriteStack&A;状态)
在System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter
1.TryWriteAsObject(Utf8JsonWriter编写器、对象值、JsonSerializerOptions、WriteStack&A;状态)
在System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Converters.IEnumerableConverter
1.OnWriteResume(Utf8JsonWriter编写器、T集合值、JsonSerializerOptions、WriteStack&A;状态)
在System.Text.Json.Serialization.JsonCollectionConverter2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter
1.TryWrite(Utf8JsonWriter编写器、T&A;值、JsonSerializerOptions、WriteStack&A;状态)
在System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter
1.OnTryWrite(Utf8JsonWriter编写器、T值、JsonSerializerOptions、WriteStack&A;状态)
在System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter
1.WriteCore(Utf8JsonWriter编写器、T&A;值、JsonSerializerOptions、WriteStack&A;状态)
-内部异常堆栈跟踪结束
在System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack&;状态下,不支持异常(Ex)
在System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter
1.WriteCoreAsObject(Utf8JsonWriter编写器、对象值、JsonSerializerOptions、WriteStack&A;状态)
在System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter、Utf8JsonWriter、TValue&;Value、JsonSerializerOptions、WriteStack&;状态)
At System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json,TValue Value,JsonTypeInfo jsonTypeInfo,CancerationToken ancellationToken)
At System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json,TValue Value,JsonTypeInfo jsonTypeInfo,CancerationToken ancellationToken)
At System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json,TValue Value,JsonTypeInfo jsonTypeInfo,CancerationToken ancellationToken)
在Microsoft.AspNetCore.Mvc.Infrastructure.SystemTextJsonResultExecutor.ExecuteAsync(ActionContext上下文中,JsonResult结果)
At Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|30_0[TFilter,TFilterAsync](资源调用器调用器,任务最后任务,下一个状态,作用域,对象状态,布尔值已完成)
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed环境中)
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,T筛选器异步](状态&A;下一个、作用域&A;范围、对象&A;状态、布尔值&A;已完成)
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
-来自前一位置的堆栈结束跟踪
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker调用方,任务最后任务、状态下一个、作用域、对象状态、布尔值isComplete)
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker调用方,任务任务,IDisposable作用域)
在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker调用方,任务任务,IDisposable作用域)
在Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint终结点,任务请求任务、日志(log)记录器)
在Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext环境中)
在Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext HttpContext上)
在Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext http上下文中,iswaggerProvider swaggerProvider)
在Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext环境中)
国家元首和政府首脑
接受度:/
连接:保持连接
Host: localhost:5291
用户代理:PostmanRuntime/7.29.2
Accept-编码:GZIP、DEFEATE、br
postman -令牌:ad516da9-02bb-424f-8979-64365d710b2c
This is my controller个
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using webApi.Models;
using Microsoft.AspNetCore.Hosting;
using System.IO;
namespace webApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ContactController : ControllerBase
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _env;
public ContactController(IConfiguration configuration, IWebHostEnvironment env)
{
_configuration = configuration;
_env = env;
}
[HttpGet]
public JsonResult Get()
{
string query = @"select ContactName from dbo.Contact";
DataTable table = new DataTable();
string sqlDataSource = _configuration.GetConnectionString("ContactAppCon");
SqlDataReader myReader;
using (SqlConnection myCon = new SqlConnection(sqlDataSource))
{
myCon.Open();
using (SqlCommand myCommand = new SqlCommand(query, myCon))
{
myReader = myCommand.ExecuteReader();
table.Load(myReader);
myReader.Close();
myCon.Close();
}
}
return new JsonResult(table);
}
}
}
This is model个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace webApi.Models
{
public class Contact
{
public int ContactID { get; set; }
public string ContactName { get; set; }
public string ContactEmail { get; set; }
public string ContactSubject { get; set; }
public string ContactMessage { get; set; }
}
}
This is startup.cs个
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Serialization;
using Microsoft.Extensions.FileProviders;
using System.IO;
namespace WebApplication1
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//Enable CORS
services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
//JSON Serializer
_ = services.AddControllersWithViews().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore)
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver
= new DefaultContractResolver());
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//Enable CORS
app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Photos")),
RequestPath = "/Photos"
});
}
}
}