MVC自定义路由01-为什么需要自定义路由
本篇体验自定义路由以及了解为什么需要自定义路由。
准备
□ View Models
using System.Collections.Generic;
namespace MvcApplication2.Models
{
//单位
public class Unit
{
public int ID { get; set; }
public RentalProperty RentalProperty { get; set; }
public string Name { get; set; }
}
//属性
public class RentalProperty
{
public int ID { get; set; }
public string Name { get; set; }
}
public class RentalPropertyTestData
{
public int ID { get; set; }
public List<RentalProperty> RentalProperties { get; set; }
public List<Unit> Units { get; set; }
}
}
.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 MvcApplication2.Models;
using System.Collections.Generic;
namespace MvcApplication2.Service
{
public class RentalService
{
public RentalPropertyTestData GetData()
{
List<RentalProperty> rps = new List<RentalProperty>();
RentalProperty rp1 = new RentalProperty() { ID = 1, Name = "长度" };
RentalProperty rp2 = new RentalProperty() { ID = 2, Name = "重量" };
rps.Add(rp1);
rps.Add(rp2);
List<Unit> units = new List<Unit>();
Unit unit1 = new Unit() { ID = 1, Name = "米", RentalProperty = rp1 };
Unit unit2 = new Unit() { ID = 2, Name = "公斤", RentalProperty = rp2 };
units.Add(unit1);
units.Add(unit2);
return new RentalPropertyTestData()
{
ID = 1,
RentalProperties = rps,
Units = units
};
}
}
}
RentalPropertiesController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Ajax.Utilities;
using MvcApplication2.Models;
using MvcApplication2.Service;
namespace MvcApplication2.Controllers
{
public class RentalPropertiesController : Controller
{
RentalPropertyTestData _data = new RentalPropertyTestData();
public RentalPropertiesController()
{
RentalService s = new RentalService();
_data = s.GetData();
}
public ActionResult All()
{
return View(_data);
}
public ActionResult RentalProperty(string rentalPropertyName)
{
var rentalProperty = _data.RentalProperties.Where(a => a.Name == rentalPropertyName).FirstOrDefault();
return View(rentalProperty);
}
public ActionResult Unit(string rentalPropertyName, string unitName)
{
var unit = _data.Units.Find(u => u.Name == unitName && u.RentalProperty.Name == rentalPropertyName);
return View(unit);
}
}
}
视图
□ All.csthml
展开@model MvcApplication2.Models.RentalProperty
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RentalProperty</title>
</head>
<body>
<div>
所选择的属性名称:@Model.Name
</div>
</body>
</html>
□ RentalProperty.cshtml
展开@model MvcApplication2.Models.RentalProperty
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RentalProperty</title>
</head>
<body>
<div>
所选择的属性名称:@Model.Name
</div>
</body>
</html>
□ Unit.cshtml
展开@model MvcApplication2.Models.Unit
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Unit</title>
</head>
<body>
<div>
所选选择的属性名称:@Model.RentalProperty.Name
<br/>
所选择的单位名称:@Model.Name
</div>
</body>
</html>
效果
All.csthml

RentalProperty.cshtml

Unit.cshtml

路由改进目标
■ http://localhost:1368/RentalProperties/All 改进为 ~/rentalproperties/
■ http://localhost:1368/RentalProperties/RentalProperty?rentalPropertyName=长度 改进为 ~/rentalproperties/rentalPropertyName/
■ http://localhost:1368/RentalProperties/Unit?rentalPropertyName=长度&unitName=米 改进为 ~/rentalproperties/rentalPropertyNam/units/unitName
添加自定义路由规则
展开using System.Web.Mvc;
using System.Web.Routing; namespace MvcApplication2
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//对应~/rentalproperties/rentalPropertyNam/units/unitName
routes.MapRoute(
name:"RentalPropertyUnit",
url: "RentalProperties/{rentalPropertyName}/Units/{unitName}",
defaults:new
{
controller = "RentalProperties",
action = "Unit"
}
); //对应~/rentalproperties/rentalPropertyName/
routes.MapRoute(
name: "RentalProperty",
url: "RentalProperties/{rentalPropertyName}",
defaults: new
{
controller = "RentalProperties",
action = "RentalProperty"
}
); //对应~/rentalproperties/
routes.MapRoute(
name: "Rental",
url: "RentalProperties",
defaults: new
{
controller = "RentalProperties",
action = "All"
}
); //默认
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
□ 效果
http://localhost:1368/RentalProperties

http://localhost:1368/RentalProperties/长度

http://localhost:1368/RentalProperties/长度/Units/米

□ 参考博文
Customizing Routes in ASP.NET MVC
MVC自定义路由01-为什么需要自定义路由的更多相关文章
- MVC路由探寻,涉及路由的惯例、自定义片段变量、约束、生成链接和URL等
引子 在了解MVC路由之前,必须了解的概念是"片段".片段是指除主机名和查询字符串以外的.以"/"分隔的各个部分.比如,在http://site.com/Hom ...
- ASP.NET MVC 路由进阶(之二)--自定义路由约束
3.自定义路由约束 什么叫自定义路由约束呢?假如路由格式为archive/{year}/{month}/{day},其中year,month,day是有约束条件的,必须是数字,而且有一定范围. 这时候 ...
- .NetCore MVC中的路由(2)在路由中使用约束
p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...
- ExtJS4.2 - 从 Hello World 到 自定义组件 -01 (为爱女伊兰奋斗)
ExtJS4.2 - 从 Hello World 到 自定义组件 - 01 经验.概述.项目搭建.国际化.HelloWorld.布局 —— 为爱女伊兰而奋斗 ——少走弯路,简单才是王道 1. 写在前面 ...
- Spring MVC 项目搭建 -6- spring security 使用自定义Filter实现验证扩展资源验证,使用数据库进行配置
Spring MVC 项目搭建 -6- spring security使用自定义Filter实现验证扩展url验证,使用数据库进行配置 实现的主要流程 1.创建一个Filter 继承 Abstract ...
- Spring MVC 项目搭建 -4- spring security-添加自定义登录页面
Spring MVC 项目搭建 -4- spring security-添加自定义登录页面 修改配置文件 <!--spring-sample-security.xml--> <!-- ...
- 7.ASP.NET MVC 5.0中的Routing【路由】
大家好,这一篇向大家介绍ASP.NET MVC路由机制.[PS:上一篇-->6. ASP.NET MVC 5.0中的HTML Helpers[HTML帮助类] ] 路由是一个模式匹配系统,它确保 ...
- 在ASP.NET MVC控制器中获取链接中的路由数据
在ASP.NET MVC中,在链接中附加路由数据有2种方式.一种是把路由数据放在匿名对象中传递: <a href="@Url.Action("GetRouteData&quo ...
- Taurus.MVC WebAPI 入门开发教程3:路由类型和路由映射。
系列目录 1.Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行. 2.Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World. 3.Tau ...
- ASP.NET Core的路由[5]:内联路由约束的检验
当某个请求能够被成功路由的前提是它满足某个Route对象设置的路由规则,具体来说,当前请求的URL不仅需要满足路由模板体现的路径模式,请求还需要满足Route对象的所有约束.路由系统采用IRouteC ...
随机推荐
- Icon.png pngcrush caught libpng error:Read
[问题处理]Icon.png pngcrush caught libpng error:Read Error 遇到问题 在项目Archive时,遇到 Icon.png pngcrush caught ...
- 利用CE手动破解百度云下载限速!
一步,你需要打开百度云<ignore_js_op> 第二步,你需要用上面附送的工具选择百度云进程<ignore_js_op> 第三点,不多说,看图<ignore_js_o ...
- Hive SQL综合案例
一 Hive SQL练习之影评案例 案例说明 现有如此三份数据:1.users.dat 数据格式为: 2::M::56::16::70072, 共有6040条数据对应字段为:UserID BigInt ...
- box-sizing 盒子模型不改变大小
参考 box-sizing 相关说明 在需要设置元素的内边距或者外边距而不想改变整体大小时,可以使用该属性,记录之. .simple { width: 500px; margin: 20px auto ...
- Oracle学习笔记——点滴汇总
Oracle学习笔记——点滴汇总 http://www.botangdb.com/ Oracle GI = Grid Infrastructure = ASM + Cluster
- 【AtCoder】AGC011 D - Half Reflector
题解 大意是n个管子排成一排,每个管子有两种状态,A状态是从某个方向进去,从原方向出来,B状态是从某个方向进去,从另一个方向出来 球经过一个A状态的管子这个管子会立刻变成B状态,经过一个B状态的管子会 ...
- 8-2 Building for UN Uva1605
题意:你的任务是设计一个包含若干层的联合国大楼,其中每层都是一个等大的网络 由若干个国家需要在联合国大楼里面办公 你需要把每个格子分配给一个国家 使得任意两个不同的国家都有一对相邻的格子 (要没是同 ...
- curl之采集QQ空间留言
目录 主要流程解析 注意事项 扩展 完整代码示例 采集效果一览 主要流程解析 首先,打开浏览器登录QQ空间并访问留言列表 由于QQ空间的链接是https,curl方式请求https链接需要突破http ...
- Ionic入门十:icon(图标)
ionic 也默认提供了许多的图标,大概有500多个.用法也非常的简单: <i class="icon ion-star"></i> 图标列表如下:   ...
- UVA - 120Stacks of Flapjacks (摊煎饼。。)排序
/* 这题使我记起了以前很多忘掉的东西,例如sstream(分割流),deque(双端队列),还有众多函数(STL里的).值得收藏 值得注意的是这题的序号问题,(因为要求输出翻转的位置),序号从右往左 ...