我最近开始学习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'
答案 0 :(得分:3)
路由您必须在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?}");
});
}