【问题标题】:ASP.NET Core hosted webapi shows "SyntaxError: JSON.parse" on GET requestASP.NET Core 托管的 webapi 在 GET 请求上显示“SyntaxError: JSON.parse”
【发布时间】:2019-12-27 16:34:49
【问题描述】:

免责声明:我刚刚开始学习 C#,并且正在尝试构建一个向 SQL 数据库发出请求的 ASP.NET 核心 Web api。两者现在都托管在 Azure 上...

...我可以通过 Postman 成功发出请求,但是在调用 Azure 上托管的 api 时无法显示可读数据。

在控制器中

[Route("api/[controller]")]
    [ApiController]
    public class TenantsController : ControllerBase
    {

        private readonly TenantContext _context;

        public TenantsController(TenantContext context) => _context = context;

        //GET: /api/tenants
        [HttpGet]
        public ActionResult<IEnumerable<Tenant>> GetTenants()
        {
            return _context.TenantDetails;
        }

在模型中

namespace TenantsApi.Models
{
    public class Tenant
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public double Rent { get; set; }
        public string Address { get; set; }
    }
}

数据库上下文

using Microsoft.EntityFrameworkCore;

namespace TenantsApi.Models
{
    public class TenantContext : DbContext
    {
        public TenantContext(DbContextOptions<TenantContext> options) : base(options)
        {
        }

        public DbSet<Tenant> TenantDetails { get; set; }
    }
}

预期结果应该是这样的:

[{"id": 1,
"Title": "Mr",
"FirstName":"John",
"Surname":"Smith",
"Email":"John@aol.com",
"Rent":500.00,
"Address":"1 Random Place, London, L1 1AA"}]

实际结果:

[

错误消息:“SyntaxError: JSON.parse: unexpected end of data at line 1 column 2 of the JSON data”只能在 Firefox 上查看

Firefox 中的标题告诉我:

内容编码 gzip 内容类型应用程序/json;字符集=utf-8 日期 2019 年 8 月 22 日星期四 10:23:25 GMT 服务器 Microsoft-IIS/10.0 传输编码分块 改变接受编码 X-Powered-By ASP.NET

【问题讨论】:

  • 在你的 GetTenant 方法中试试这个:return Ok(_context.TenantDetails)
  • 请问我可以询问从哪个命名空间“OK”获取吗?
  • 它来自您的TenantsController 继承的ControllerBase 类。它的类型是:[NonAction]public virtual OkResult Ok();
  • 你搞定了吗?
  • 不幸的是我没有,抱歉,我对 C# 很陌生,是否需要使用 'using' 来添加命名空间?

标签: c# json syntax-error azure-sql-database asp.net-core-webapi


【解决方案1】:

https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-dotnetcore-sqldb

如果您是 c# 和 Azure 的新手,这对您来说是一个很好的快速入门。

你也可以使用下面的 github repo:

https://github.com/Azure-Samples/dotnet-core-api

以下是一些需要注意的要点:

  • 请确保为托管 API 启用 CORS。 您可以参考以下文档以供参考:

https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-rest-api

  • 默认情况下,SQL Azure 数据库只允许来自 Azure 应用或资源的连接。如果您尝试从本地计算机访问它,请确保为其添加防火墙规则:

请按照以下文档进行操作:

https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-dotnet-sqldatabase

请告诉我进展如何。希望对您有所帮助。

【讨论】:

  • 嗨,已经按照第一个教程进行操作,并且可以启动并运行它,但是当将服务逻辑转移到我的本地存储库时,它似乎没有翻译。第三个教程也试过了,感觉两个天蓝色的资源可能通信不正常,SQL Server和Web应用的连接字符串等都配置好了,还是不行
  • 我不知道你把逻辑转移到本地仓库是什么意思?
  • 在 ASP.NET Core 教程的生产中连接到 SQL Db 部分之后添加了以下内容对此仍然非常新,所以我不太确定我知道连接在做什么。在配置时添加了 App.UseCors() 但到目前为止还没有工作 ``` public void ConfigureServices(IServiceCollection services){if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Production") services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection"))); } ```
  • 对此仍然非常陌生,所以我不太确定我知道连接配置在后台做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 2019-04-08
  • 2018-09-22
相关资源
最近更新 更多