话不多少 直接上代码
通常我们以为上边的是解决控制同名问题,是解决了一点,但是又出了以下问题,默认请求的不是项目默认的控制器而是该区域的控制器,在我之前开发的项目中,默认指向的是区域下的home控制器,然而昨天在同事的.net mvc 4中碰到一个问题,总是打开网站的时候打开的是后台,而不是默认的项目下的home控制器,前些日子我在做开发的时候,碰到一些大牛说,我写的只对了一半,于是大牛给我改了就可以了,看下边RouteConfig.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Blog
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
//项目默认请求项目的默认控制器
namespaces: new string[] { "Blog.Controllers" }
);
}
}
}
还有Max(区域下的设置)MaxAreaRegistration.cs
using System.Web.Mvc;
namespace Blog.Areas.Max
{
public class MaxAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Max";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Max_default",
"Max/{controller}/{action}/{id}",
new {Controller="Home", action = "Index", id = UrlParameter.Optional },
//让该区域默认请求区域下的控制器
new[] { "Blog.Areas.Max.Controllers" }
);
}
}
}
以上两个部分的代码,均解决控制器同名问题和请求错误问题,,,如果大家按照图一上的来写,调试代码的时候进不去控制后台,目前我测试 的有 mvc 4和3 其他的没有测试
因本人忙于项目:如需联系我的加QQ:35924908 就说是看到我博客园加的