I have created a Rest API that works (its returning the data I expect in postman).
But when I try to go the swagger part I get this error:
Fetch error undefined /swagger/v1/swagger.json
Swagger Error Message:
Here is the code in my startup class
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "TP_Service", Version = "v1" });
});
services.AddDbContext<CommandContext>(opt =>
opt.UseSqlServer(Configuration["Data:CommandAPIConnection:ConnectionString"]));
services.AddMvc(option => option.EnableEndpointRouting =
false).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}
and
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment() || env.IsProduction())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestApi v1"));
}
app.UseMvc();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
What have I done wrong to make it not work. (It does when I run the Code on IIS Express)
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TP_Service.Models;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace TP_Service.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ServiceController : ControllerBase
{
private readonly CommandContext _context;
public ServiceController(CommandContext context)
{
_context = context;
}
// GET: api/<ServiceController>
[HttpGet]
public ActionResult<IEnumerable<Service>> GetServices()
{
return _context.Services;
}
// GET api/<ServiceController>/5
[HttpGet("{id}")]
public ActionResult<ServiceVM> GetServiceDetails(int id)
{
ServiceVM serviceVM = new ServiceVM();
serviceVM.service = _context.Services.Find(id);
serviceVM.ServiceTaskList = _context.ServiceTasks.Select(
x => new ServiceTask()
{
Id = x.Id,
ServiceId = x.ServiceId,
CurrentAssemblyID = x.CurrentAssemblyID,
CurrentAssemblyName = x.CurrentAssemblyName,
TaskName = x.TaskName,
TaskQuoteName = x.TaskQuoteName,
TaskDescription = x.TaskDescription,
Notes = x.Notes,
ServiceTaskPartList = _context.ServiceTaskParts.Select(
x => new ServiceTaskPart()
{
Id = x.Id,
TaskId = x.TaskId,
PartId = x.PartId,
PartName = x.PartName,
QtyNeeded = x.QtyNeeded,
QtyUsed = x.QtyUsed,
QtyReturned = x.QtyReturned,
QtyLeftOnSite = x.QtyLeftOnSite
}
).Where(STP => STP.TaskId == x.Id).ToList()
}
).Where(ST => ST.ServiceId == id).ToList();
return serviceVM;
}
//// POST api/<ServiceController>
//[HttpPost]
// public void Post([FromBody] string value)
// {
// }
// // PUT api/<ServiceController>/5
// [HttpPut("{id}")]
// public void Put(int id, [FromBody] string value)
// {
// }
// // DELETE api/<ServiceController>/5
// [HttpDelete("{id}")]
// public void Delete(int id)
// {
// }
//}
}