MVC 路由路径设置的问题

Squall001 2018-03-04 06:26:38
我的目的是 需要访问指定目录下的控制器,意思就是 http://www.a.com/XXX/controller/action
这个XXX就是指定的目录,
我现在的操作是,在routeconfig.cs文件内,默认路由上面添加了个自定义的路由,如下

routes.MapRoute(
name: "BG",
url: "BG/{controller}/{action}/{id}",
defaults: new { Controller = "Admin", action = "Index", id = UrlParameter.Optional },
constraints: new { Url = @"?=BG/.*" }
//constraints: new { Controller = "Admin",url=@"?=BG/.*" }
);

然后重载了个razorview的类
如下

public class LayUIViewEngine:RazorViewEngine
{
public LayUIViewEngine()
{
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
//以上两个是默认视图引擎路径
"~/Views/BG/{1}/{0}.cshtml"//我们的规则
};

}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
return base.FindView(controllerContext, viewName, masterName, useCache);
}

}


然后在global.asax里面 注册了这个view的类

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

RegisterView(); //注册视图子目录
}

protected void RegisterView()
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new LayUIViewEngine());
}
}


现在 通过目录 http://www.a.com/BG/controller/action,可以访问得到这个目录下的控制器了,
但是,同样可以http://www.a.com/controller/action,也可以访问到对应的控制,
所以现在的我的问题是 可不可以通过routes.MapRoute里面的constraints参数,设定这个访问条件必须是URL在"BG/"开头的,才进入对应的控制器
...全文
735 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2018-03-05
  • 打赏
  • 举报
回复
#2正解,使用 Area就好了。
Squall001 2018-03-05
  • 打赏
  • 举报
回复
引用 2 楼 starfd 的回复:
Area区域 http://blog.csdn.net/hanjun0612/article/details/61918960
好的,最终还是用到Areas了。。。
resuozhi0021 2018-03-05
  • 打赏
  • 举报
回复
#2正解,使用 Area就好了。
ajpitnhhyj 2018-03-05
  • 打赏
  • 举报
回复
#2正解,使用 Area就好了
Squall001 2018-03-05
  • 打赏
  • 举报
回复
现在我用了个简单方法来实现。就是自定义路由 首先添加两个自定义的路由限制的类

    public class UserAgentConstraint : IRouteConstraint
    {
        public UserAgentConstraint()
        {
        }
        public bool Match(HttpContextBase httpContext, Route route, string parameterName,
                            RouteValueDictionary values, RouteDirection routeDirection)
        {
            return httpContext.Request.Url.ToString().Contains("/BG/") &&
                (
                    values["Controller"].ToString().Equals("Admin",StringComparison.CurrentCultureIgnoreCase) ||
                    values["Controller"].ToString().Equals("editor", StringComparison.CurrentCultureIgnoreCase)
                );
        }
    }

    public class defaultConstraint : IRouteConstraint
    {
        public defaultConstraint()
        {
        }
        public bool Match(HttpContextBase httpContext, Route route, string parameterName,
                            RouteValueDictionary values, RouteDirection routeDirection)
        {
            return values["Controller"].ToString().Equals("Home",StringComparison.CurrentCultureIgnoreCase);
        }
    }
以上代码定义了两种限制,一种是访问路径里面包含了"/BG/"这个字段并且控制器必须为admin和editor的,另外一个是查看控制器必须为"Home"的 然后把注册路由的函数改为这样:

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

            routes.MapRoute(
                name: "BG",
                url: "BG/{controller}/{action}/{id}",
                defaults: new { Controller = "Admin", action = "Index", id = UrlParameter.Optional },
                constraints: new { customConstraint = new UserAgentConstraint()},
                namespaces: new string[] { "GGM.Controllers.BG" }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { customConstraint = new defaultConstraint() },
                namespaces: new string[] { "GGM.Controllers" }                
            );
        }
这样就可以实现了,意思就是如果要匹配到BG那条路由,必须满足UserAgentConstraint 这个类里面定义的条件,下面那个默认路由,必须满足defaultConstraint 这个类的条件。。 但是这样一来就麻烦了,如果要加控制器,那么必须要在对应类的约束条件下多加一条限制语句。。这样其实在多人开发一个项目的时候很不方便。。导致每次提交代码都冲突,git上面需要合并分支才可以,感觉容易出错。。。 那么请问如何才能简单的实现这个功能?

62,241

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧