简体   繁体   English

使用“dotnet new react -o my-app”中的默认代码时,为什么会出现 JSON.parse 错误?

[英]Why do I get JSON.parse error when using default code from “dotnet new react -o my-app”?

Running the terminal command 'dotnet new react -o my-app' builds a C# React app, but when testing the starting files it doesn't work.运行终端命令 'dotnet new react -o my-app' 会构建一个 C# React 应用程序,但是在测试启动文件时它不起作用。 Running 'npm start' from the my-app/ClientApp directory starts the app but navigating to the page http://localhost:3000/fetch-data you get the console error:从 my-app/ClientApp 目录运行“npm start”会启动应用程序,但导航到页面http://localhost:3000/fetch-data会出现控制台错误:

"SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data" “语法错误:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符”

This is the block of code that it never gets past:这是它永远不会通过的代码块:

constructor(props) {
super(props);
this.state = { forecasts: [], loading: true };

fetch('api/SampleData/WeatherForecasts')
  .then(response => response.json())
  .then(data => {
    this.setState({ forecasts: data, loading: false });
  });
}

It calls this controller, but the code in it never runs:它调用此 controller,但其中的代码从未运行:

[Route("api/[controller]")]
public class SampleDataController : Controller
{
    [HttpGet("[action]")]
    public IEnumerable<WeatherForecast> WeatherForecasts()
    {
        Console.WriteLine("I NEVER GET CALLED");
        var rng = new Random();
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            DateFormatted = DateTime.Now.AddDays(index).ToString("d"),
            TemperatureC = rng.Next(-20, 55),
            Summary = Summaries[rng.Next(Summaries.Length)]
        });
    }

I'm using these docs to build the app:我正在使用这些文档来构建应用程序:

https://docs.microsoft.com/en-us/aspnet/core/client-side/spa/react?view=aspnetcore-3.0&tabs=netcore-cli https://docs.microsoft.com/en-us/aspnet/core/client-side/spa/react?view=aspnetcore-3.0&tabs=netcore-cli

I plan to use this as a scaffold to build a new app, but I'm concerned that it isn't working before I've done any coding to it.我计划将其用作构建新应用程序的脚手架,但我担心在我对其进行任何编码之前它无法正常工作。 Does anyone know why or how to fix it?有谁知道为什么或如何解决它?

You get the error because NODEJS (NPM START) isn't designed to host .NET, and the API call written in c#, is AspNet, AspNetCore, etc. As an aside, NPM START kicks off resources located in the package.json file, and that file makes no reference to the Microsoft portion of the overall solution (yes the Microsoft article from which you are working is someone confusing since it doesn't directly point that out). You get the error because NODEJS (NPM START) isn't designed to host .NET, and the API call written in c#, is AspNet, AspNetCore, etc. As an aside, NPM START kicks off resources located in the package.json file,并且该文件未提及整体解决方案的 Microsoft 部分(是的,您正在工作的 Microsoft 文章令人困惑,因为它没有直接指出这一点)。

IIS (or in this case Microsoft Internet Information Services Express ) can host both React and the API because the Startup.cs file and AspNetCore are configured to do so. IIS (或在本例中为Microsoft Internet Information Services Express )可以同时托管React和 API,因为Startup.cs文件和AspNetCore已配置为这样做。

Real world, we typically generate our API's under one project, while building our UI application under another, each hosted on a unique Port, which at a minimum, offers a big plus by being able to test the API / UI independently (well, the API independently anyway - you can test the UI without the API as well but without API data, it's not always practical).在现实世界中,我们通常在一个项目下生成我们的 API,同时在另一个项目下构建我们的 UI 应用程序,每个都托管在一个唯一的端口上,这至少通过能够独立测试API / UI来提供很大的优势(嗯, API 无论如何都独立 - 您也可以在没有 API 但没有 API 数据的情况下测试 UI,这并不总是实用的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM