MVC 4区域路由不起作用

Waq*_*ees 10 c# asp.net-mvc asp.net-mvc-routing c#-4.0 asp.net-mvc-4

我创建了一个空的MVC4应用程序,一切正常,之后我将一个区域添加到我的项目名为"主持人".我的区域路由代码是这样的:

using System;
using System.Web.Mvc;

namespace EskimoArt.Areas.Moderator
{
    public class ModeratorAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Moderator";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Moderator_default",
                "Moderator/{controller}/{action}/{id}",
                new {controller="Dashboard", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Global.asx代码是这样的:

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace EskimoArt
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但现在我想访问

> http://localhost/Moderator/Dashboard
Run Code Online (Sandbox Code Playgroud)

它显示像这样的错误页面

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Moderator

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
Run Code Online (Sandbox Code Playgroud)

Jes*_*ess 13

检查namespace控制器.

我已经将一些代码移动到一个区域,并且命名空间仍然显示旧位置.

ReSharper有一个非常酷的选项来修复文件夹,项目或解决方案中所有文件的命名空间!解决这个问题非常方便.

  • 更具体地说,检查控制器中的命名空间是否匹配`MapRoute`中的已注册命名空间.像`namespace Project.Web.Areas.Admin.Controllers`这样的东西匹配`namespaces:new [] {"Project.Web.Areas.Admin.Controllers"}` (3认同)

小智 6

我有同样的问题.您查看App_Start/RouteConfig.cs并添加此代码的顶部AreaRegistration.RegisterAllAreas();

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        AreaRegistration.RegisterAllAreas();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

创建创建区域/主持人/控制器/ DashboardController.cs

public class DashboardController : Controller
{
    //
    // GET: /Moderator/Dashboard/

    public ActionResult Index()
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

并创造

地区/主持人/浏览/仪表板/ Index.cshtml

您还需要在Areas/Moderator/Views/Web.config中拥有Web.config ...