MVC自定义路由02-实现IRouteConstraint限制控制器名
通过实现IRouteConstraint接口,实现对某个控制名进行限制。本篇把重点放在自定义约束,其余部分参考:
自定义约束前
using System.Web.Mvc;
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 }
);
}
}
}
.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;
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);
}
}
}
.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;
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") }
);
}
}
}
.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名称的控制器将被限制。
MVC自定义路由02-实现IRouteConstraint限制控制器名的更多相关文章
- ASP.NET MVC 自定义路由中几个需要注意的小细节
本文主要记录在ASP.NET MVC自定义路由时,一个需要注意的参数设置小细节. 举例来说,就是在访问 http://localhost/Home/About/arg1/arg2/arg3 这样的自定 ...
- ASP.NET MVC自定义路由 - 实现IRouteConstraint限制控制器名(转载)
自定义约束前 namespace MvcApplication2 { public class RouteConfig { public static void RegisterRoutes(Rout ...
- MVC自定义路由01-为什么需要自定义路由
本篇体验自定义路由以及了解为什么需要自定义路由. 准备 □ View Models using System.Collections.Generic; namespace MvcApplicati ...
- MVC自定义路由实现URL重写,SEO优化
//App_Start-RouteConfig.cs public class RouteConfig { public static void RegisterRoutes(RouteCollect ...
- Mvc自定义路由让支持.html的格式
前言 在大多时候,我们都需要自定义路由,当我们设置为/list/1.html的时候,有的时候会出现以下异常. routes.MapRoute( "category", // 路由名 ...
- Asp.net MVC 自定义路由在IIS7以上,提示Page Not Found 解决方法
受限确保自定义路由在开发服务器上Ok! 然后在web.config的<webserver>节点下增加如下配置就好了. 1: <system.webServer> 2: &l ...
- MVC 自定义路由
RouteConfig.cs 代码如下: public class RouteConfig { public static void RegisterRoutes(RouteCollection ro ...
- vs2017 mvc 自定义路由规则 出现 404.0 错误代码 0x80070002
自定义: WebApiConfig 里面最后增加 config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttp ...
- Asp.net MVC 自定义路由
在做公司接口的时候 由于规范API 要用点分割. 如: HealthWay.controller.action 在MVC 4 下面做了个 路由配置如下: public override void R ...
随机推荐
- hdu 5943(素数间隔+二分图匹配)
Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- SCTF 2015 pwn试题分析
Re1 是一个简单的字符串加密.程序使用了多个线程,然后进行同步.等加密线程加密好了之后才会启动验证线程.这个题比较坑的是IDA F5出来的结果不对,不知道是不是混淆机制. 刚开始看的是F5后的伪代码 ...
- 20165333 实验二 Java面向对象程序设计
姓名:陈国超 学号:20165333 班级:1653 实验课程:JAVA程序设计 实验名称:Java面向对象程序设计 实验时间:2018.4.14 指导老师:娄家鹏 实验内容及步骤 (一) " ...
- javascript输入验证数字方法,适合充值时输入正整数验证
说明:用于验证正整数的输入,不允许输入其他字符. html: <input type="text" id="sell_jobNum" name=" ...
- XUtils开源框架的使用(HttpUtils支持多线程断点续传)
XUtils项目下载地址:https://github.com/wyouflf/xUtils XUtils中包含的四大模块: 1.DbUtils模块 2.ViewUtils模块 3.HttpUtils ...
- Windows下的Apache
https://blog.csdn.net/weixin_39082031/article/details/79088800
- Ionic实战一:Ionic仿照微信项目
github下载地址:https://github.com/Frogmarch/ionic-wechat 博客学习地址:http://www.cnblogs.com/Frogmarch/ Ionic仿 ...
- COCO 数据集使用说明书
下面的代码改写自 COCO 官方 API,改写后的代码 cocoz.py 被我放置在 Xinering/cocoapi.我的主要改进有: 增加对 Windows 系统的支持: 替换 defaultdi ...
- 变量覆盖漏洞学习及在webshell中的运用
一.发生条件: 函数使用不当($$.extract().parse_str().import_request_variables()等) 开启全局变量 二.基础了解: 1.$$定义 $$代表可变变量, ...
- 【原创】MySQL Replay线上流量压测工具
一. 背景 去年做过一次mysql trace 重放的测试,由于performance schema本身采集样本的长度等限制,实际回放的成功率比较低. 最近找到一款开源的工具,基于TCPCopy实现了 ...