MVC 4区域路由不起作用

时间:2021-05-26 11:21:34

I created an empty MVC4 Application, all things are working fine, after that I add an Area to my Project Named "Moderator". My area routing code is like this:

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

And my Global.asx code is like this:

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

But now I want to access the

但现在我想访问

> http://localhost/Moderator/Dashboard

It shows an Error Page like this

它显示像这样的错误页面

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

5 个解决方案

#1


6  

I had the same problem. You look to App_Start/RouteConfig.cs and add top this code AreaRegistration.RegisterAllAreas();

我有同样的问题。您查看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 }
        );
    }
}

Create create Areas/Moderator/Controllers/DashboardController.cs

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

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

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

and create

并创造

Areas/Moderator/Views/Dashboard/Index.cshtml

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

You also need to have Web.config in Areas/Moderator/Views/Web.config ...

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

#2


9  

Check the namespace of your controller.

检查控制器的命名空间。

I had moved some code into an area and the namespace was still showing the old location.

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

ReSharper has a very cool option to fix the namespaces of all files in the folder, project or solution! It is very handy to fix this.

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

#3


1  

Just try to delete content from following directories as mentioned here and rebuild project

只是尝试删除此处提到的以下目录中的内容并重建项目

C:\Temp C:\Users\%Username%\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 Files Path\To\Your\Project\obj\Debug

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 Files Path \ To \ Your \ Project \ obj \ Debug

#4


0  

Just add the "namespace:" named parameter in RegisterRoutes() method of RouteConfig.cs in App_Start folder. And specify the value of "namespace" to the namespace of your controller, within which you want to call action method. In following example i want to call Index() method in Village Controller from root of project.

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

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Village", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "MvcApplication1.Controllers" }
        );
    }

#5


0  

Make sure that in project properties > Web > Start Action > if a specific page | action | url is defined, it does match with mapped routes.

确保在项目属性> Web>启动操作>中,如果是特定页面|行动| url已定义,它与映射的路由匹配。

#1


6  

I had the same problem. You look to App_Start/RouteConfig.cs and add top this code AreaRegistration.RegisterAllAreas();

我有同样的问题。您查看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 }
        );
    }
}

Create create Areas/Moderator/Controllers/DashboardController.cs

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

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

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

and create

并创造

Areas/Moderator/Views/Dashboard/Index.cshtml

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

You also need to have Web.config in Areas/Moderator/Views/Web.config ...

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

#2


9  

Check the namespace of your controller.

检查控制器的命名空间。

I had moved some code into an area and the namespace was still showing the old location.

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

ReSharper has a very cool option to fix the namespaces of all files in the folder, project or solution! It is very handy to fix this.

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

#3


1  

Just try to delete content from following directories as mentioned here and rebuild project

只是尝试删除此处提到的以下目录中的内容并重建项目

C:\Temp C:\Users\%Username%\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 Files Path\To\Your\Project\obj\Debug

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 Files Path \ To \ Your \ Project \ obj \ Debug

#4


0  

Just add the "namespace:" named parameter in RegisterRoutes() method of RouteConfig.cs in App_Start folder. And specify the value of "namespace" to the namespace of your controller, within which you want to call action method. In following example i want to call Index() method in Village Controller from root of project.

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

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Village", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "MvcApplication1.Controllers" }
        );
    }

#5


0  

Make sure that in project properties > Web > Start Action > if a specific page | action | url is defined, it does match with mapped routes.

确保在项目属性> Web>启动操作>中,如果是特定页面|行动| url已定义,它与映射的路由匹配。