0

I would like to have second controller in my asp.net WebApi, but when i add it it not works... First Controller works OK

i have 404 not found in my browser

  • not any errors while run

whats wrong?

namespace testing.Controllers
   {
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering",           
   "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

and the second is below

{
    [Route("[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public int Get()
        {
            return 100050;
        }
    }
}

Can some one tell me whats wrong?

10
  • 1
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details which can be done with a minimal reproducible example. Please edit your question to add these details into it or we may not be able to help. Commented Apr 22, 2021 at 22:21
  • i have 404 not found ;/ Commented Apr 22, 2021 at 22:21
  • What url did you use? Commented Apr 22, 2021 at 22:21
  • 2
    Take off the word "Controller" from your url. See more here: learn.microsoft.com/en-us/aspnet/core/mvc/controllers/… Commented Apr 22, 2021 at 22:23
  • 1
    ...... sorry im beginner ... you can give it as answer, i will give you best Commented Apr 22, 2021 at 22:26

2 Answers 2

2

You're using the attribute [Route("[controller]")] on your controller class. The string [controller] means "the name of the class, without the actual WORD "Controller".

This means, the name of the controller is "Values" (or "WeatherForecast" for the previous controller).

So, the final url route you want is /Values, not /ValuesController.

You can read more about how this works on the MS Docs page (the whole page has a lot of good information, not just that section).

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

Comments

0

There is another web server here that will forward request /weatherforecast to kestrel web server which host your controller.

Edit /ClientApp/setupProxy.js, change /weatherforecast to /api/**

const context =  [
  "/api/**",
];

Edit Program.cs, add api/ to the path

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

Edit MyController.cs, add api/ to route

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

Alternatively, you could remove the SpaProxy and serve the static pages directly from the kestrel web server.

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.