we are trying to implement the new asp.net core web api with swagger.
The issue I am having is that the swagger UI shows my TestString call as follows :
This is how the call looks form the controller :
[ApiController]
//[Route("[controller]/[action]")]
public class ObjectController : ControllerBase
{
private readonly IApplicationDBContext _context;
public ObjectController(IApplicationDBContext context)
{
_context = context;
}
[Route("api/object/TestString")]
[HttpGet]
public string TestString()
{
return "awe";
}
}
But as soon as I click on the call to test it, it just shows me it is loading and it is not hitting my breakpoint on the return of the method, also no errors are being thrown.
Below is my Startup.cs file in the project :
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen();
services.AddDbContext<NORDbContext>(x => x.UseSqlServer(Configuration.GetConnectionString("ConnStr")));
services.AddScoped(provider => provider.GetService<IApplicationDBContext>());
}
// 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.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
I have also tried changing the routes on the controller and on the methods, but to no avail.
I have also tried changing the swagger endpoint by adding the "./" and removing it.
I am hoping it is something small I am overlooking, but so far nothing I googled has really helped me solve the problem.
fix the route, and IMHO change the controller name. It is not the best idea to use reserved words in the custom class or variables names. there are plenty another words
[Route("~/api/test/TestString")]
public string TestString()
{
return "awe";
}