c# - MVC 4 区域路由不工作

标签 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

最佳答案

检查 Controller 的命名空间

我将一些代码移到了一个区域中,但命名空间仍显示旧位置。

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

关于c# - MVC 4 区域路由不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19239995/

相关文章:

c# - Dock->Top 控件的顺序有问题

c# - ViewModel 在操作方法中获取空值

asp.net-mvc - 在 MVC 和 WCF 应用程序中共享 ASP.Net 应用程序缓存

c# - 奇怪的异常编译动态构建表达式

c# - 如何以编程方式 Kindlize .HTML 或 .DOCX 或 .PDF 文件(使其适合 Kindle)?

c# - 如何获取正则表达式来检查字符串是否仅包含字母字符 [a-z] 或 [A-Z]?

.net - 为什么即使我不调用服务的 Dispose() 方法也会调用它? (使用 BasicHttpBinding)

c# - 如何使用 XPath 选择两个节点之间的所有元素

c# - MVC3 路由奇怪的行为

asp.net-mvc - 在 ASP.NET MVC 中的 Html.ListBox 中需要帮助