- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习如何使用 ASP.NET Core 创建 React 应用程序。作为一个新手,我从头开始尝试让“Hello World”显示在主页上。我使用 Visual Studio 的默认 React.js 项目模板来开始。路由设置为默认值。这是我的文件:
首页.js:
import React, { Component } from 'react';
export class Home extends Component {
constructor(props) {
super(props);
this.state = { message: "" };
fetch('api/Home/Message')
.then(response => response.json())
.then(data => {
this.setState({ message: data });
});
}
render () {
return (
<h1>{this.state.message}</h1>
);
}
}
HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace TestingReactDotNet.Controllers
{
[Route("api/[controller]")]
public class HomeController : Controller
{
[HttpGet]
public async Task<IActionResult> Message()
{
var response = "Hello World";
return Ok(response);
}
}
}
问题是解析为 Json 的 HTTP 响应不正确。为了尝试调试,我已经将它的 console.logged 出来,看起来 response.json())
正在检索默认 public/index.html
中的所有文本模板应用程序附带的文件。有谁知道这是为什么?
如果我遗漏了一些非常明显的东西,我深表歉意 - 我使用的是 Mac,因此文件结构和 Visual Studio IDE 非常不同,我很难理解已经存在的许多教程/答案。
最佳答案
要点击您的 Message()
函数,您必须将 HttpGet 发送到 'api/Home'
而不是 'api/Home/Message'
.
如果您希望您的端点是 'api/Home/Message'
,那么您必须为 Message()
函数指定路由,如下所示:
// api/Home/Message
[HttpGet, Route("Message")]
public async Task<IActionResult> Message()
关于c# - .NET Core React 项目中的 MVC 路由没有选择我的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57516250/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!