Home > Software engineering >  Getting page not found trying to navigate to my asp.net core 5.0 web service
Getting page not found trying to navigate to my asp.net core 5.0 web service

Time:09-22

I create a new ASP.NET Core Razor Project using .Net 5.0, I have created a simple Web service API page which is below. The issue is when I run it and then in the browser I call http://localhost:50050/api/test I get "HTTP ERROR 404*" meaning my web service cannot be found. I have a similar application just done in older version of .net core and that works just fine. Does anyone know what I am doing wrong?

using Microsoft.AspNetCore.Mvc;

namespace WebApplication2.Api
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        [HttpGet]
        public int GetTest()
        {
            return 21;
        }

        [HttpGet("{id}")]
        public int GetTest([FromRoute] int id)
        {
            return 22;
        }
    }
}

The start up page is shown below:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication2
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
        }

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

            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}

Below is my project layout

enter image description here

CodePudding user response:

You only have an endpoint to your RazorPages, not with your Controllers.

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });

what you need to do is to register your Controllers in the Startup ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
     services.AddControllers();
}

then add those Controllers in your Endpoint Middleware like so:

public void Configure(IApplicationBuilder app){
   // other middlewares
   app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
            });
}
  • Related