ASP.Net核心MVC项目无法配置路由?

时间:2016-10-25 09:50:07

标签: asp.net-core asp.net-core-mvc asp.net-core-webapi

我最近开始学习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'

1 个答案:

答案 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?}");
            });
        }