来自不同Controller的Dotnet核心路由返回404

时间:2016-12-15 16:21:14

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

我对dotnet core 1.0有疑问。我需要在不同的项目(和不同的命名空间)中使用来自Controller的路由。怎么可能这样做?

这是我在运行项目时的创业公司:

    public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            //this line I've tried to add (CmsController is from different project, linked to startup project)
            services.AddMvc().AddApplicationPart(typeof(CmsController).GetTypeInfo().Assembly);

            services.AddCms<ApplicationDbContext>(Configuration, true);

            // Add application services.
            services.AddTransient<IEmailSender, AuthMessageSender>();
            services.AddTransient<ISmsSender, AuthMessageSender>();
        }

这是简单的CmsController:

 public class CmsController : Controller
    {
        public ICmsService CmsService { get; protected set; }

        public CmsController(ICmsService cmsService)
        {
            this.CmsService = cmsService;
        }

        [Route("")]          
        private async Task<IActionResult> Index()
        {
            return await this.ReturnResult();
        }
      ...
}

在我的路线扩展程序中,我有类似的内容:

public static Action<IRouteBuilder> Register()
        {
            return routes =>
            {
                routes.MapRoute(
                    "cmsRoute",
                    "{area:exists}/{controller=Home}/{action=Index}");

                routes.MapRoute(
                    "cmsFrontend",
                    "/{controller=Cms}/{action=Index}",
                    new {controller = "CmsController", action = "Index", Area = string.Empty});
            };
        }

在主项目中注册启动方法:

    app.UseMvc(routes =>
    {                       
        CmsRouteExtensions.Register();

        SeedRouteExtensions.Register();

    });  

当我尝试在主项目中运行HomeController时,一切正常。但是当我在没有HomeController的情况下运行它(对此注释索引操作和注释路由)时,我得到了404.但我应该看到根域中的CmsController路由(主页)http://localhost/

请问哪里有问题?

谢谢!

0 个答案:

没有答案