Meh*_*aki 19 c# asp.net-mvc .net-core asp.net-core
我有一个Asp.Net core 2.2项目。
最近,我将版本从.net core 2.2更改为.net core 3.0 Preview8。更改之后,我看到以下警告消息:
使用端点路由时,不支持使用“ UseMvc”配置MVC。要继续使用“ UseMvc”,请在“ ConfigureServices”中设置“ MvcOptions.EnableEndpointRouting = false”。
我了解通过将其设置EnableEndpointRouting为false可以解决此问题,但是我需要知道什么是解决问题的正确方法,以及为什么端点路由不需要UseMvc()功能。
Ser*_*kyi 19
我在以下官方文档“ 从ASP.NET Core 2.2迁移到3.0 ”中找到了对我的小型项目有帮助的解决方案:
将UseMvc或UseSignalR替换为UseEndpoints。
就我而言,结果看起来像这样
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//Old way
services.AddMvc();
//Or new recommended way
//services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}");
});
}
}
Run Code Online (Sandbox Code Playgroud)
Aka*_*ani 14
我发现的问题是由于 .NET Core 框架的更新造成的。最新的 .NET Core 3.0 发布版本需要显式选择加入以使用 MVC。
当尝试从较旧的 .NET Core(2.2 或预览版 3.0 版本)迁移到 .NET Core 3.0 时,此问题最为明显
如果从 2.2 迁移到 3.0,请使用以下代码解决问题。
services.AddMvc(options => options.EnableEndpointRouting = false);
Run Code Online (Sandbox Code Playgroud)
如果使用 .NET Core 3.0 模板,
services.AddControllers(options => options.EnableEndpointRouting = false);
Run Code Online (Sandbox Code Playgroud)
修复后的 ConfigServices 方法如下,
谢谢你
Md.*_*lom 13
ASP.NET 5.0 上默认禁用端点路由
按照启动时的配置即可
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options => options.EnableEndpointRouting = false);
}
Run Code Online (Sandbox Code Playgroud)
这对我有用
NHA*_*Med 11
您可以在ConfigureServices方法中使用::
services.AddControllersWithViews();
Run Code Online (Sandbox Code Playgroud)
对于配置方法:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
但我需要知道什么是解决问题的正确方法
通常,您应该使用EnableEndpointRouting代替UseMvc,并且可以参考更新路由启动代码以获得启用的详细步骤EnableEndpointRouting。
为什么端点路由不需要UseMvc()函数。
对于UseMvc,它使用the IRouter-based logic和EnableEndpointRouting使用endpoint-based logic。它们遵循以下不同的逻辑:
if (options.Value.EnableEndpointRouting)
{
var mvcEndpointDataSource = app.ApplicationServices
.GetRequiredService<IEnumerable<EndpointDataSource>>()
.OfType<MvcEndpointDataSource>()
.First();
var parameterPolicyFactory = app.ApplicationServices
.GetRequiredService<ParameterPolicyFactory>();
var endpointRouteBuilder = new EndpointRouteBuilder(app);
configureRoutes(endpointRouteBuilder);
foreach (var router in endpointRouteBuilder.Routes)
{
// Only accept Microsoft.AspNetCore.Routing.Route when converting to endpoint
// Sub-types could have additional customization that we can't knowingly convert
if (router is Route route && router.GetType() == typeof(Route))
{
var endpointInfo = new MvcEndpointInfo(
route.Name,
route.RouteTemplate,
route.Defaults,
route.Constraints.ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value),
route.DataTokens,
parameterPolicyFactory);
mvcEndpointDataSource.ConventionalEndpointInfos.Add(endpointInfo);
}
else
{
throw new InvalidOperationException($"Cannot use '{router.GetType().FullName}' with Endpoint Routing.");
}
}
if (!app.Properties.TryGetValue(EndpointRoutingRegisteredKey, out _))
{
// Matching middleware has not been registered yet
// For back-compat register middleware so an endpoint is matched and then immediately used
app.UseEndpointRouting();
}
return app.UseEndpoint();
}
else
{
var routes = new RouteBuilder(app)
{
DefaultHandler = app.ApplicationServices.GetRequiredService<MvcRouteHandler>(),
};
configureRoutes(routes);
routes.Routes.Insert(0, AttributeRouting.CreateAttributeMegaRoute(app.ApplicationServices));
return app.UseRouter(routes.Build());
}
Run Code Online (Sandbox Code Playgroud)
对于EnableEndpointRouting,它使用EndpointMiddleware将请求路由到端点。
小智 7
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//Old Way
services.AddMvc();
// New Ways
//services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseRouting();
app.UseCors();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}");
});
}
}
Run Code Online (Sandbox Code Playgroud)
这也适用于 .Net Core 5
-> 在ConfigureServices方法中-Startup.cs
//*****REGISTER Routing Service*****
services.AddMvc();
services.AddControllers(options => options.EnableEndpointRouting = false);
Run Code Online (Sandbox Code Playgroud)
-> 在配置方法中 - Startup.cs
//*****USE Routing*****
app.UseMvc(Route =>{
Route.MapRoute(
name:"default",
template: "{Controller=Name}/{action=Name}/{id?}"
);
});
Run Code Online (Sandbox Code Playgroud)
小智 5
这对我有用(在Startup.cs中添加)
services.AddMvc(option => option.EnableEndpointRouting = false)
| 归档时间: |
|
| 查看次数: |
8811 次 |
| 最近记录: |