Reputation: 458
I've started learning asp.net core and trying to improve my c# knowledge.
I'm struggling with ConfigureServices in my startup class.
public void ConfigureMvcOptions(MvcOptions options)
{
options.EnableEndpointRouting = false;
}
public void ConfigureServices(IServiceCollection services)
{
Action<MvcOptions> options = new Action<MvcOptions>(ConfigureMvcOptions);
services.AddMvc(options);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello world");
});
}
This above is resulting in a compile error
"Warning MVC1005 Using 'UseMvcWithDefaultRoute' to configure MVC is not supported while using Endpoint Routing. To continue using 'UseMvcWithDefaultRoute', please set 'MvcOptions.EnableEndpointRouting = false' inside 'ConfigureServices'"
I can remove the error by using a Lambda expression inside the AddMvc method instead but I don't understand why this is working
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc((options) => options.EnableEndPointRouting = false);
}
The above works without a problem. I just don't understand what's wrong with my implementation of the Action delegate?
Upvotes: 2
Views: 421
Reputation: 93173
There's a Roslyn Analyzer that checks for the case where you've used either UseMvc
or UseMvcWithDefaultRoute
, but you haven't set EnableEndPointRouting
to false
. Unfortunately, this analyzer isn't smart enough to detect that your ConfigureMvcOptions
method does set EnableEndPointRouting
to false
.
This means the only way to avoid this warning is to set the value inline, as you've done using the fix you've shown. It's worth noting that the recommended setup for using MVC with ASP.NET Core 3+ is to use endpoint routing, rather than disabling it.
Upvotes: 1