我已经在.net core 3.0 Web api项目中运行了很长时间,没有出现问题。
我刚刚从nuGet存储库中安装了Microsoft.AspNet.OData。我相信我肯定在配置文件中犯了一个错误。
OData正常运行,但是我的swashbuckle中间件无法运行。
以下代码: 注意:我知道还有其他中间件比较草率,但是在添加有关oData的行之前,所有中间件都可以正常工作。
我已经尝试过弄乱语法的顺序,但是没有任何运气。我知道是什么在破坏它,但是很难描述它。
namespace MyProjectPortalVueJs
{
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.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAd(options =>
{
Configuration.Bind("AzureAd", options);
AzureAdOptions.Settings = options;
})
.AddCookie();
// Add framework services.
services.AddMvc()
//UNSURE WHAT THE COMPATIBILITY WAS DOING. CHANGED ON 4-16-2019
//.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
.AddSessionStateTempDataProvider();
services.AddSession();
// Simple example with dependency injection for a data provider.
services.AddSingleton<Providers.IWeatherProvider, Providers.WeatherProviderFake>();
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
services.AddOData();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// 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/v1/swagger.json", "My API V1");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
// Webpack initialization with hot-reload.
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
{
HotModuleReplacement = true,
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseSession(); // Needs to be before app.UseAuthentication() and app.UseMvc() otherwise you will get an exception "Session has not been configured for this application or request."
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.EnableDependencyInjection();
routes.Expand().Select().Count().OrderBy().Filter();
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
}
}
}