Having previously worked in .NET Framework, I'm now getting started with .NET Core.
I am building an application that is both MVC and Web API, and some of my routing is not working.
Here is my controller code:
[Route("api/[controller]")]
[ApiController]
public class ClientController : ControllerBase
{
private readonly ApplicationDbContext db;
public ClientController(ApplicationDbContext context)
{
db = context;
}
[HttpGet]
public ActionResult Get()
{
return Ok("get works");
}
[HttpGet]
public ActionResult Test()
{
return Ok("test not working");
}
[HttpGet]
[Route("api/client/input")]
public ActionResult input(int id)
{
return Ok(string.Format("Your id: {0}", id));
}
}
And here is my startup.cs code:
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.AddDbContext<ApplicationDbContext>(opts => opts.UseSqlServer(Configuration.GetConnectionString("sqlConnection")));
services.AddMvc();
}
// 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("/Home/Error");
// 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.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
- https://localhost:5001/api/client works
- https://localhost:5001/api/client/test 404 error
- https://localhost:5001/api/client/2 404 error
- https://localhost:5001/api/client?id=2 404 error
I have tried adding specific routes to my individual actions and that had no effect.
Thanks in advance.