Whatever page I try to load after running my api, I get the "This localhost page can't be found" 404 error, even if I try to run a method that only has an "Ok" return. I'm thinking the issue might lie in the way I configured my app. That, or I made some errors while routing.
I've tried messing around with app configuration, looking at examples, but most of them have additional NuGet packages installed. I tried using the .AddControllers()
method in the ConfigureServices
method, which resulted in a syntax error, I tried using .UseRouting()
in my Configure
function, and again, I was met with a syntax error.
Startup class:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IRepositoryWrapper, RepositoryWrapper>();
services.AddDbContext<ArtGalleriesContext>(
options=>options.UseSqlServer(
Configuration.GetConnectionString("Database")));
//services.AddScoped<IArtItemRepository1, ArtItemRepository1>();
services.AddMvc();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// 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
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Program class:
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
I am trying to call this function
[Route("api/[controller]")]
[ApiController]
public class AdminController : ControllerBase
[HttpGet("/user/add/{num1}/")]
public IActionResult SumActionResult(int num1)
{
return Ok(num1 );
}
like this: api/admin/users/add/1 and have it display 1 in my web page to confirm functionality. I am getting the "This localhost page can't be found" error instead.
You need something like this
[HttpGet("user/add/{num1}")]
public IActionResult SumActionResult(int num1)
{
return Ok(num1 );
}
Also note that in route your have user not 'users'. For further details you can read https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2
Your problem is here.
[HttpGet("/user/add/{num1}/")]
Because of the preceding slash ( /
), the route is just /user/add/{num1}
, not /api/admin/user/add/{num1}
. Remove the preceding slash:
[HttpGet("user/add/{num1}/")]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.