MVC 4区域路由不起作用

时间:2013-10-08 05:47:03

标签: c# asp.net-mvc c#-4.0 asp.net-mvc-4 asp.net-mvc-routing

我创建了一个空的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 }
            );
        }
    }
}

我的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);
        }
    }
}

但现在我想访问

> http://localhost/Moderator/Dashboard

它显示像这样的错误页面

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

6 个答案:

答案 0 :(得分:12)

检查控制器的namespace

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

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

答案 1 :(得分: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 }
        );
    }
}

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

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

    public ActionResult Index()
    {
        return View();
    }
}

并创建

区/主持人/查看/仪表板/ Index.cshtml

您还需要在Areas / Moderator / Views / Web.config中使用Web.config ...

答案 2 :(得分:1)

尝试从here提及的以下目录中删除内容并重建项目

  

C:\ Temp C:\ Users \%用户名%\ AppData \ Local \ Microsoft \ VisualStudio   C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Temporary ASP.NET Files   C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ Temporary ASP.NET   文件路径\到\ Your \ Project \ obj \ Debug

答案 3 :(得分:0)

只需在RegisterRoutes()文件夹中RouteConfig.cs的{​​{1}}方法中添加“namespace:”命名参数即可。并在控制器的命名空间中指定“namespace”的值,在其中要调用action方法。 在下面的示例中,我想从项目的根目录中调用Village Controller中的App_Start方法。

Index()

答案 4 :(得分:0)

确保在项目属性>网络>开始行动>如果是特定的页面|行动| url已定义,它与映射的路由匹配。

答案 5 :(得分:0)

最好添加

  

AreaRegistration.RegisterAllAreas();

在global.asax.cs中。您必须按照以下顺序放置。

GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

请注意,在注册路由之后,RegisterAllAreas()应该是一件事。 即。 RouteConfig.RegisterRoutes(RouteTable.Routes);应该在AreaRegistration.RegisterAllAreas();之前。我已经尝试和测试了,并且对我有用。

有关更多信息:enter link description here