0

I created my dotnet 7 react application using:

dotnet new react -o my-new-app

Based on: https://learn.microsoft.com/en-us/aspnet/core/client-side/spa/react?preserve-view=true&tabs=netcore-cli&view=aspnetcore-7.0

Now, following:

https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-controller?view=aspnetcore-7.0&tabs=visual-studio-code

I tried to add my controller called UserInfoController.cs (created within the Controllers folder)

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";
    }
}

Yet, whenever I run the link that makes the call

 https:// ..... /UserInfo

I get the response:

No routes matched location "/UserInfo" 

The application is running anyway. I also have:

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

Am I missing something?

2
  • Try https://.../User instead. If that doesn't work, look at what you have set up for app.MapControllerRoute. Commented Nov 27, 2023 at 22:23
  • I changed User to UserInfo and still having this problem (please, see updated question). I wonder if there is a problem with the AppRoutes.js file. Commented Nov 27, 2023 at 22:56

1 Answer 1

2

You need to add the routes to the setupProxy.js in the "clientApp"
For example, I create an api controller to return string with route "/api/values/test"

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

Go to client App open the "setupProxy.js"
enter image description here
Add "/api/values/test" into the "context" Arrary
enter image description here
Now you could visit the route with no problem.
enter image description here

You could also proxy the whole "values" controller like this
enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.