asp.net 在React dotnet 7应用程序中添加控制器

3zwtqj6y  于 5个月前  发布在  .NET
关注(0)|答案(1)|浏览(49)

我创建了我的dotnet 7 react应用程序,使用:

dotnet new react -o my-new-app

字符串
基于:https://learn.microsoft.com/en-us/aspnet/core/client-side/spa/react?preserve-view=true&tabs=netcore-cli&view=aspnetcore-7.0
现在,以下是:
https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-controller?view=aspnetcore-7.0&tabs=visual-studio-code
我尝试添加名为UserInfoController.cs的控制器(在Controllers文件夹中创建)

using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;

namespace my-new-app.Controllers;

public class UserInfoController : Controller
{
    public string Index()
    {
        return "Default Action for User Controller";
    }

    public string Welcome()
    {
        return "The is the Welcome Action";
    }
}


然而,每当我运行调用的链接时,

https:// ..... /UserInfo


我得到的回应是:

No routes matched location "/UserInfo"


应用程序仍然在运行。我还有:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");


我错过了什么吗?

8wigbo56

8wigbo561#

您需要将路由添加到“clientApp”中的setupProxy.js
例如,我创建了一个API控制器来返回路由为“/API/values/test”的字符串

[Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpGet("test")]
        public string test()
        {
            return "Successfully call the backend controller";
        }
    }

字符串
转到客户端应用程序,打开“setupProxy.js”
x1c 0d1x的数据
"/api/values/test"添加到“上下文”数组中



现在你可以毫无问题地参观路线。



您也可以像这样代理整个“values”控制器


相关问题