Asp.net core ASP.Net核心MVC项目无法配置路由?
我最近开始学习ASP.NETCore来开发MVCWebAPI。但我甚至无法运行我的hello world 在Asp.net core ASP.Net核心MVC项目无法配置路由?,asp.net-core,asp.net-core-mvc,asp.net-core-webapi,Asp.net Core,Asp.net Core Mvc,Asp.net Core Webapi,我最近开始学习ASP.NETCore来开发MVCWebAPI。但我甚至无法运行我的hello world 在Startup.cs中,我写道: public void ConfigureServices(IServiceCollection services) { services.AddRouting(); services.AddMvc(routes => { routes.MapRoute( "default",
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'
您必须添加
配置
方法而不是配置服务
。试试这个样品
这里将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?}");
});
}