ASP.Net Core MVC 项目无法配置路由?
ASP.Net Core MVC Project cannot configure routes?
我最近开始学习 ASP.Net Core 来开发 MVC Web API。但是我什至无法得到我的世界运行.
在 Startup.cs
我写道:
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
services.AddMvc(routes =>
{
routes.MapRoute(
"default",
"{version?}/{controller=Home}/{action=Index}/{id?}");
});
}
控制台显示:
error CS1929: 'MvcOptions' does not contain a definition for 'MapRoute' and the best extension method overload 'MapRouteRouteBuilderExtensions.MapRoute(IRouteBuilder, string, string)' requires a receiver of type 'IRouteBuilder'
路由 您必须在 Configure
方法中添加,而不是 ConfigureService
。试试这个示例。
这里添加mvc到服务
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
此处设置路线
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
我最近开始学习 ASP.Net Core 来开发 MVC Web API。但是我什至无法得到我的世界运行.
在 Startup.cs
我写道:
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();
services.AddMvc(routes =>
{
routes.MapRoute(
"default",
"{version?}/{controller=Home}/{action=Index}/{id?}");
});
}
控制台显示:
error CS1929: 'MvcOptions' does not contain a definition for 'MapRoute' and the best extension method overload 'MapRouteRouteBuilderExtensions.MapRoute(IRouteBuilder, string, string)' requires a receiver of type 'IRouteBuilder'
路由 您必须在 Configure
方法中添加,而不是 ConfigureService
。试试这个示例。
这里添加mvc到服务
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
此处设置路线
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}