MVC 4区路由不工作 [英] MVC 4 Area Routing is not working

查看:115
本文介绍了MVC 4区路由不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个空MVC4应用程序,所有的东西都工作正常,后,我一个区域添加到我的项目命名为主持人我的区域路由code就是这样

 使用系统;
使用System.Web.Mvc;命名空间EskimoArt.Areas.Moderator
{
    公共类ModeratorAreaRegistration:AreaRegistration
    {
        公众覆盖字符串AREANAME
        {
            得到
            {
                返回主持人;
            }
        }        公共覆盖无效RegisterArea(AreaRegistrationContext上下文)
        {
            context.MapRoute(
                Moderator_default
                主持人/ {控制器} / {行动} / {ID}
                新{控制器=仪表盘,行动=索引,ID = UrlParameter.Optional}
            );
        }
    }
}

和我的Global.asx code就是这样

 使用System.Web.Http;
使用System.Web.Mvc;
使用System.Web.Optimization;
使用System.Web.Routing;命名空间EskimoArt
{
    //注意:有关启用IIS6 IIS7或经典模式的指令,
    //参观http://go.microsoft.com/?LinkId=9394801
    公共类MvcApplication:System.Web.HttpApplication
    {
        保护无效的Application_Start()
        {
            AreaRegistration.RegisterAllAreas();            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

但现在我想访问

 > HTTP://本地主机/主持人/仪表板

这表明这样一个错误页面

 在'/'应用程序的服务器错误。资源无法找到。说明:HTTP 404。您正在寻找(或它的一个依赖)可能已被删除的资源,更名或暂时不可用。请检查以下URL并确保其拼写正确。请求的URL:/主持人版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.17929


解决方案

我有同样的问题。你看App_Start / RouteConfig.cs并添加这顶code AreaRegistration.RegisterAllAreas();

 公共类RouteConfig
{
    公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);
        AreaRegistration.RegisterAllAreas();        routes.MapRoute(
            名称:默认,
            网址:{控制器} / {行动} / {ID}
            默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
        );
    }
}

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

 公共类DashboardController:控制器
{
    //
    // GET:/主持人/仪表板/    公众的ActionResult指数()
    {
        返回查看();
    }
}

和创建

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

您还需要有在Web.config中区/主持人/查看/ Web.config中...

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 that

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 that

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 show an Error Page like that

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

解决方案

I had the same problem. You look to App_Start/RouteConfig.cs and add top this code 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

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

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

and create

Areas/Moderator/Controllers/Views/Dashboard/Index.cshtml

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

这篇关于MVC 4区路由不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆