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 ...
随机推荐
- ZOJ 3781 Paint the Grid Reloaded(DFS连通块缩点+BFS求最短路)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5268 题目大意:字符一样并且相邻的即为连通.每次可翻转一个连通块X( ...
- C++之插入迭代器
#include<iostream> #include<vector> #include<list> #include<iterator> usingn ...
- Maven3核心技术(笔记三)
第一节:Maven仓库概念 Maven 远程仓库配置文件:$M2_HOME/lib/maven-model-builder-3.3.3.jar 文件:org\apache\maven\model\po ...
- 按书上学写测试pytest
慢慢的,这块知识也补好吧. 系统的学习框架,具体的细节,可以边百度边实现. test_three.py '''Test the Task data type.''' from collections ...
- Linux--忘记MySQL密码的解决方法和输入mysqld_safe --skip-grant-tables &后无法进入MySQL的解决方法
https://blog.csdn.net/qq_35389417/article/details/78910974
- IEEEXtreme 极限编程大赛题解
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 IEEEXtreme全球极限编程挑战赛,是由IEEE主办,IEEE学生分会组织承办.IEEE会员参与指导和监督的.IEEE学生会员以团队 ...
- php读取xml中cdata部分方法
本例使用php的simplexml:XML(eventtrackdata.xml'): <eventdata> <event> <date>2012.05.11&l ...
- echarts3.0 本期累计堆叠
@{ ViewBag.Title = "barlj"; } <h2>barlj</h2> <div id="main" style ...
- 【LOJ】#2066. 「SDOI2016」墙上的句子
题解 我一直也不会网络流--orz 我们分析下这道题,显然和行列没啥关系,就是想给你n + m个串 那么我们对于非回文单词之外的单词,找到两两匹配的反转单词(即使另一个反转单词不会出现也要建出来) 具 ...
- Maven 仓库之阿里云镜像配置
每当项目开发中 update Maven Project 的时候,我们会发现那个进度是非常的慢,这也严重阻碍了平日的开发进度. 然而,殊不知阿里云搭建了一个国内镜像 http://mav ...