I am using ASP.NET Core 2.1 Web API with Angular 6 as frontend.
I have added OData functionality to the Startup.cs
file in the Web API project. I also use Swagger to document the API.
This is the code in the Startup.cs
file for ConfigureServices
:
public void ConfigureServices(IServiceCollection services)
{
services.AddOData();
services.AddCors();
services.AddMvc(opt =>
{
opt.UseGeneralRoutePrefix("BookManagement");
opt.SslPort = SettingsLoader<AppSettings>.Settings.SslPort;
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ;
services.AddMvcCore(options =>
{
foreach (var outputFormatter in options.OutputFormatters.OfType<OutputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
{
outputFormatter.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
foreach (var inputFormatter in options.InputFormatters.OfType<InputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
{
inputFormatter.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
}
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddRequestScopingMiddleware(() => _scopeProvider.Value = new Scope());
services.AddCustomControllerActivation(Resolve);
services.AddCustomViewComponentActivation(Resolve);
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc($"v{AppVersion.cVersion}", new Info { Title = "Books Microservice", Version = $"v{AppVersion.cVersion}" });
c.IncludeXmlComments(Path.Combine(Environment.CurrentDirectory, "Synergy.AssetsModule.WebApi.xml"));
});
}
Configure
method is as below
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
Kernel = RegisterApplicationComponents(app);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
if (SettingsLoader<AppSettings>.Settings.AutoUpdateDatabase)
{
IDatabaseLogic databaseLogic = new DatabaseLogic(Kernel.Get<IDatabasePersistence>());
databaseLogic.UpgradeDatabase();
}
InitiateMessageReprocess();
#if !NO_AUTH
ISingleSignOnLogic singleSignOnLogic = Kernel.Get<ISingleSignOnLogic>();
app.UseMiddleware<AuthenticationFilter>(singleSignOnLogic);
#endif
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"/swagger/v{AppVersion.cVersion}/swagger.json", $"Books Microservice API v{AppVersion.cVersion}");
c.RoutePrefix = string.Empty;
});
app.UseCors(builder =>
{
builder.AllowAnyOrigin();
builder.AllowAnyMethod();
builder.AllowAnyHeader();
});
app.UseMvc(routeBuilder =>
{
routeBuilder.EnableDependencyInjection();
routeBuilder.Expand().Select().Count().OrderBy().Filter();
});
app.UseRewriter(new RewriteOptions().AddRedirectToHttps(StatusCodes.Status301MovedPermanently, SettingsLoader<AppSettings>.Settings.SslPort));
LogProvider.Instance.LogInformation("Started application");
}
With this code, the Swagger API is returning 204 Undocumented.
Don't know where it is going wrong. The parameter content type previously I was using is application/json-patch json
. Please suggest why I am not getting the data from the Web API method.
CodePudding user response:
A couple of things. First, I would suggest creating a separate web service to host your OData endpoints. Second, prior to .net 6, it's somewhat difficult to get OData and Swagger to work together. Even in .net 6, there are a few things missing when it comes to swagger.
I have an open-source project that fills in the gaps. Here's a sample project that illustrates how to use it.
https://github.com/ikemtz/NRSRx/tree/master/samples/IkeMtz.Samples.OData