question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Update SampleDataController in blazorhosted template to use standard ApiController conventions

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mkArtakMSFTcommented, Jun 25, 2019

Not really, @isaac2004. @pranavkm has already fixed this as part of https://github.com/aspnet/AspNetCore/pull/11351

0reactions
isaacrlevincommented, Jun 25, 2019

@mkArtakMSFT did you close the wrong issue?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found