问题描述
我刚刚更新到ASP.NET Core 3 Preview 5,现在当我打开解决方案并尝试构建它时会引发错误在Configure()中的Startup.cs文件中,方法'UseRouting'的方法不重载1个参数",如下代码:
I just updated to ASP.NET Core 3 Preview 5, now when I open my solution and try to build it throws the error'No overload for method 'UseRouting' takes 1 arguments' in the Startup.cs file in the Configure() at the following code:
app.UseRouting(routes => {
routes.MapControllerRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRazorPages();
});
我确实阅读了有关Microsoft文档的一些文档,并尝试将上述代码替换为:
I did read some documentation on the Microsoft docs and tried replacing the above code with:
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
但是,在构建时抛出具有以下上下文的System.InvalidOperationException:
However, during build time that throws an System.InvalidOperationException with the following context:
我尝试替换ConfigureServices方法中的以下行:
I tried replacing the following line in the ConfigureServices method:
services.AddMvc()
.AddNewtonsoftJson();
宽度:
services.AddControllersWithViews()
.AddNewtonsoftJson();
services.AddRazorPages();
这不再引发错误,但加载完成后我的页面为空白.谁可以帮助我解决这个问题?
This does not throw errors anymore but my page is blank when it finishes loading. Who can help me solve this issue?
对于我的解决方案,我使用以下软件包:
For my solution I use the following packages:
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview5.19227.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview5.19227.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0-preview5.19227.9" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
我的解决方案的TargetFramework是netcoreapp3.0
The TargetFramework of my solution is netcoreapp3.0
推荐答案
再次引用错误消息:
ASP.NET Core 3使用完善的端点路由,它通常会为应用程序内的路由提供更多控制.端点路由分为两个独立的步骤:
ASP.NET Core 3 uses a refined endpoint routing which will generally give more control about routing within the application. Endpoint routing works in two separate steps:
- 第一步,再对请求的路由进行匹配,然后再对已配置的路由进行匹配,以找出正在访问的路由. 在最后的步骤中,对确定的路由进行评估,并使用相应的中间件,例如称为MVC.
- In a first step, the requested route is matched agains the configured routes to figure out what route is being accessed.
- In a final step, the determined route is being evaluated and the respective middleware, e.g. MVC, is called.
这是两个单独的步骤,以允许其他中间件在这些点之间起作用.这允许那些中间件利用来自端点路由的信息,例如来处理授权,而不必作为实际的 handler (例如MVC)的一部分来执行.
These are two separate steps to allow other middlewares to act between those points. That allows those middlewares to utilize the information from endpoint routing, e.g. to handle authorization, without having to execute as part of the actual handler (e.g. MVC).
这两个步骤由app.UseRouting()
和app.UseEndpoints()
设置.前者将注册运行逻辑以确定路由的中间件.然后,后者将执行该路由.
The two steps are set up by app.UseRouting()
and app.UseEndpoints()
. The former will register the middleware that runs the logic to determine the route. The latter will then execute that route.
如果您在EndpointRoutingMiddleware
和EndpointMiddleware
的用法有些混乱的情况下仔细阅读错误消息,它将告诉您在Configure
方法内部添加UseRouting()
.因此,基本上,您忘记了调用端点路由的第一步.
If you read the error message carefully, between the somewhat confusing usage of EndpointRoutingMiddleware
and EndpointMiddleware
, it will tell you to add UseRouting()
inside of the Configure
method. So basically, you forgot to invoke the first step of endpoint routing.
因此,您的Configure
应该(例如)看起来像这样:
So your Configure
should (for example) look like this:
app.UseRouting();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
这篇关于方法'UseRouting'的重载没有1个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!