jsonreaderexception:分析值时遇到意外字符:< 路径“”,第0行,位置0

u2nhd7ah  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(289)

我试图寻找类似的线索,但我似乎找不到解决我的问题的办法。
我有asp.net web api和客户端uwp应用程序,通过该api与本地主机数据库通信。
以下是获取数据的代码(客户端应用程序):

using (var client = new HttpClient())
        {
            var response = "";
            Task task = Task.Run(async () =>
            {
                response = await client.GetStringAsync(App.BaseUri); // sends GET request
            });
            task.Wait(); // Wait

            List<Product> ProductList = JsonConvert.DeserializeObject<List<Product>>(response); //ERROR HERE XX

            listViewAPI.ItemsSource = ProductList; // Bind the list
        }

我的应用程序中的baseuri文件如下所示:

public static Uri BaseUri = new Uri("http://localhost:58834/api/Product/"); // base API URL; ProductController

以下是api中的控制器:

using ASPNetWebAPI.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ASPNetWebAPI.Controllers
{
[Route("api/Product")]
public class ProductController : ApiController
{
    // GET: api/product
    [HttpGet]
    public IEnumerable<Product> Get() 
    {
        return Model.ReadAllSQLProducts();
    }

}
}

以下是api中的模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ASPNetWebAPI.Models
{
public class Model
{

    // Example data source
    private static List<Product> products = new List<Product>()
    {

    };

    // R part of CRUD
    public static List<Product> ReadAllSQLProducts()
    {
        return products;
    }

}

}

以下是api中的实际产品类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ASPNetWebAPI.Models
{
public class Product
{
    public int Id { get; set; }
    public string CreationDate { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }

}
}

下面是api的web.config中的连接字符串:

<configuration>
  <connectionStrings>
<add name="ProductConn" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=ProductDB;Integrated Security=True;MultipleActiveResultSets=True"/>  
  </connectionStrings> 
...
</configuration>

我在connectionstring中显式地告诉了连接到哪个db,因为我在localhost中有多个数据库。
我无法解决以下错误:
引发异常:newtonsoft.json.dll中的“newtonsoft.json.jsonreaderexception”newtonsoft.json.jsonreaderexception类型的异常发生在newtonsoft.json.dll中,但未在用户代码中处理分析值时遇到意外字符:<。路径“”,第0行,位置0。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题