14

I created a ASP.NET Core Razor page app (asp.net version 2.1.1). It works just fine with the normal Pages but I also want an ApiController as in this tutorial: https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-vsc?view=aspnetcore-2.1

However, when I create my controller just as in the example above, I get a 404 page whenever I try to reach it.

Is there something I am missing from the startup class?

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<DomainDbContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc();
        }

And my apicontroller class:

    [Route("api/[controller]")]
    [ApiController]
    class DomainController : ControllerBase 
    {
        private readonly DomainDbContext _context;

        public DomainController (DomainDbContext context) 
        {
            _context = context;
        }

        [HttpGet]
        public ActionResult<List<Domain>> GetAll()
        {
            return new List<Domain> {new Domain() {Name ="Hello", Tld = ".se", Expiration = DateTime.UtcNow.AddDays(35)}};
        }
}

Everything looks like the guides as far as I can see, but obviously something is not correct since I get 404 for all pages. Even if I create a new method it doesn't really quite work as intended and is unreachable.

The main path I've tried is /api/domain.

Thanks for your help in advance!

8
  • 2
    Maybe public class DomainController
    – Mate
    Commented Jun 25, 2018 at 20:45
  • 1
    @mate Seems too obvious now.. thanks. Make it an answer and I'll accept it.
    – Ms01
    Commented Jun 25, 2018 at 20:49
  • 1
    Fresh eyes :) @CodeNotFound already added it
    – Mate
    Commented Jun 25, 2018 at 20:51
  • 1
    @Mate :D didn't see your comments. I was adding my answer and formating it while you already add your comment at 20h45. My answer at 20h47 :D Commented Jun 25, 2018 at 20:53
  • 1
    @CodeNotFound The important thing is that the code is working now ;)
    – Mate
    Commented Jun 25, 2018 at 20:56

2 Answers 2

11

Just add endpoints.MapControllers() to the UseEndpoints options:

app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
            });
1
  • The MapControllers also fixed .NET 6, thanks!
    – Cydaps
    Commented Mar 5, 2023 at 19:19
8

You need to have a public controller class.

So instead of:

[Route("api/[controller]")]
[ApiController]
class DomainController : ControllerBase
{
    [...]
} 

You should have this:

[Route("api/[controller]")]
[ApiController]
public class DomainController : ControllerBase // <-- add a public keyword
{
    [...]
} 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.