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 ...
随机推荐
- 本地删除文件,git远程不同步删除
git add -a 或 git add * 它能stages所有文件,包括之前删除的痕迹 git add . 只能stages新文件和被修改的文件,不会stages已被删除的文件 步骤如下: 1) ...
- Linux学习笔记:wc查看文件字节数、字数、行数
Linux系统中的wc(Word Count)命令可以统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 若不指定文件名称,或是所给予的文件名为“-”,则wc指令会从标准输入设备读取数据. 语 ...
- element-ui 2.7.2版本使用 表格展开行 功能遇到的奇葩问题?
在使用 element-ui 2.7.2版本的时候报下面的错误: [Vue warn]: Error in callback for watcher "data": "E ...
- deploy.sh
备份一下之前的一个脚本吧 #!bin/bash adb uninstall org.cocos2d.fishingjoy4 for apk in `find . -name '*.apk' | xar ...
- iOS模块器截屏闪退
最近不知道什么原因,iOS模块器截屏命令点击模拟器就闪退,在此记录下在命令行截屏操作: 第一步:打开对应的模拟器 第二步:模拟器缩放比为100% 第三步:输入以下命令,001.jpg为要保存的文件名 ...
- Python3语法详解
一.下载安装 1.1Python下载 Python官网:https://www.python.org/ 1.2Python安装 1.2.1 Linux 平台安装 以下为在Unix & Linu ...
- ASP.NET MVC5+ 路由特性
概述 ASP.NET MVC 5支持一种新的路由协议,称为路由特性. MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 案例 1.使用Visual Studio 20 ...
- Haproxy 开启日志记录
CentOS 7上yum安装的Haproxy,默认没有记录日志.需要做一下配置才能记录日志.(不知道其他版本是否需要,已经忘记了)主要是用到了Haproxy,以前貌似没有这么麻烦,今天配置出了一些问题 ...
- 开始Hibernate介绍
1.介绍 一个框架 一个Java领域内的持久化框架 一个ORM框架 2.持久化 和数据库相关的各种操作 保存 更新 删除 查询 加载:根据特定的OID,把一个对象从数据库加载到你内存中. OID:为了 ...
- 1017 Queueing at Bank (25)(25 point(s))
problem Suppose a bank has K windows open for service. There is a yellow line in front of the window ...