Update SampleDataController in blazorhosted template to use standard ApiController conventions
See original GitHub issueThe SampleDataController
in the blazorhosted template doesn’t follow our latest ApiController conventions.
Current code:
[Route("api/[controller]")]
public class SampleDataController : Controller
{
private static string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet("[action]")]
public IEnumerable<WeatherForecast> WeatherForecasts()
{
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)]
});
}
}
Expected code:
[Route("api/SampleData/[controller]")]
[ApiController]
public class WeatherForecastsController : ControllerBase
{
private static string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet]
public ActionResult<IEnumerable<WeatherForecast>> GetWeatherForecasts()
{
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)]
}).ToList();
}
}
We should also update the routing setup in the Server project to call MapControllers
instead of MapDefaultControllerRoute
and call AddControllers
instead of AddMvc
.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Use web API conventions
A convention allows you to: Define the most common return types and status codes returned from a specific type of action. Identify actions...
Read more >Routing Conventions in ASP.NET Web API 2 Odata
Describes routing conventions that Web API 2 in ASP.NET 4.x uses for OData endpoints.
Read more >Web API Controllers
Web API controller is a class which can be created under the Controllers folder ... Put, Patch or Delete as shown in the...
Read more >Web API Routing
It routes an incoming HTTP request to a particular action method on a Web API controller. Web API supports two types of routing:...
Read more >Blazor WebAssembly & Web API on .NET 6 – Full Course (C#)
Learn Blazor WebAssembly and Web API on .NET 6 by building a shopping cart application using C#. This course also provides a guide...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Not really, @isaac2004. @pranavkm has already fixed this as part of https://github.com/aspnet/AspNetCore/pull/11351
@mkArtakMSFT did you close the wrong issue?