您的位置:首页 > 大数据 > 人工智能

MVC自定义路由02-实现IRouteConstraint限制控制器名

2014-03-11 11:14 453 查看
通过实现IRouteConstraint接口,实现对某个控制名进行限制。本篇把重点放在自定义约束,其余部分参考:

MVC自定义路由01-为什么需要自定义路由 

  自定义约束前

using System.Web.Mvc;


[code]using System.Web.Routing;


using MvcApplication2.Extension;


 


namespace MvcApplication2


{


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 }


);


}


}


}


 

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

效果





 

  自定义约束后

□ 实现IRouteConstraint接口

using System;


[code]using System.Web.Routing;


 


namespace MvcApplication2.Extension


{


public class ExcludeController : IRouteConstraint


{


private readonly string _controller;


 


public ExcludeController(string controller)


{


_controller = controller;


}


public bool Match(System.Web.HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)


{


//如果路由中拿到的controller值与约束设定的值相等,就返回false


return !string.Equals(values["controller"].ToString(), _controller, StringComparison.OrdinalIgnoreCase);


}


}


}


 

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

□ 路由添加约束

using System.Web.Mvc;


[code]using System.Web.Routing;


using MvcApplication2.Extension;


 


namespace MvcApplication2


{


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 },


constraints: new { controller = new ExcludeController("RentalProperties") }


);


}


}


}


 

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

效果





可见,加上自定义约束后,带RentalProperties名称的控制器将被限制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: