本篇体验自定义路由以及了解为什么需要自定义路由。

准备

□ 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-为什么需要自定义路由的更多相关文章

  1. MVC路由探寻,涉及路由的惯例、自定义片段变量、约束、生成链接和URL等

    引子 在了解MVC路由之前,必须了解的概念是"片段".片段是指除主机名和查询字符串以外的.以"/"分隔的各个部分.比如,在http://site.com/Hom ...

  2. ASP.NET MVC 路由进阶(之二)--自定义路由约束

    3.自定义路由约束 什么叫自定义路由约束呢?假如路由格式为archive/{year}/{month}/{day},其中year,month,day是有约束条件的,必须是数字,而且有一定范围. 这时候 ...

  3. .NetCore MVC中的路由(2)在路由中使用约束

    p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...

  4. ExtJS4.2 - 从 Hello World 到 自定义组件 -01 (为爱女伊兰奋斗)

    ExtJS4.2 - 从 Hello World 到 自定义组件 - 01 经验.概述.项目搭建.国际化.HelloWorld.布局 —— 为爱女伊兰而奋斗 ——少走弯路,简单才是王道 1. 写在前面 ...

  5. Spring MVC 项目搭建 -6- spring security 使用自定义Filter实现验证扩展资源验证,使用数据库进行配置

    Spring MVC 项目搭建 -6- spring security使用自定义Filter实现验证扩展url验证,使用数据库进行配置 实现的主要流程 1.创建一个Filter 继承 Abstract ...

  6. Spring MVC 项目搭建 -4- spring security-添加自定义登录页面

    Spring MVC 项目搭建 -4- spring security-添加自定义登录页面 修改配置文件 <!--spring-sample-security.xml--> <!-- ...

  7. 7.ASP.NET MVC 5.0中的Routing【路由】

    大家好,这一篇向大家介绍ASP.NET MVC路由机制.[PS:上一篇-->6. ASP.NET MVC 5.0中的HTML Helpers[HTML帮助类] ] 路由是一个模式匹配系统,它确保 ...

  8. 在ASP.NET MVC控制器中获取链接中的路由数据

    在ASP.NET MVC中,在链接中附加路由数据有2种方式.一种是把路由数据放在匿名对象中传递: <a href="@Url.Action("GetRouteData&quo ...

  9. Taurus.MVC WebAPI 入门开发教程3:路由类型和路由映射。

    系列目录 1.Taurus.MVC WebAPI  入门开发教程1:框架下载环境配置与运行. 2.Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World. 3.Tau ...

  10. ASP.NET Core的路由[5]:内联路由约束的检验

    当某个请求能够被成功路由的前提是它满足某个Route对象设置的路由规则,具体来说,当前请求的URL不仅需要满足路由模板体现的路径模式,请求还需要满足Route对象的所有约束.路由系统采用IRouteC ...

随机推荐

  1. SQL Server 管理常用的SQL和T-SQL

    1. 查看数据库的版本 select @@version 常见的几种SQL SERVER打补丁后的版本号: 8.00.194 Microsoft SQL Server 2000 8.00.384 Mi ...

  2. CentOS7的firewall和安装iptables

    前言:CentOS7 的防火墙默认使用是firewall,而我们通常使用iptables: 本文记录了firewall基础的命令和iptables的安装和使用. firewall部分: part1 : ...

  3. HBase(十)HBase性能调优总结

    一. HBase的通用优化 1 高可用 在 HBase 中 Hmaster 负责监控 RegionServer 的生命周期,均衡 RegionServer 的负载,如果 Hmaster 挂掉了,那么整 ...

  4. day7 socket网络编程基础

    Socket Socket是什么? 下面来看一下网络的传输过程: 上面图片显示了网络传输的基本过程,传输是通过底层实现的,有很多底层,我们写传输过程的时候,要知道所有的过程那就太复杂了,socket为 ...

  5. 【LOJ】#2574. 「TJOI2018」智力竞赛

    题解 二分答案 求最小路径点覆盖 由于这里最小路径点覆盖,点是可重的,用floyd求出传递闭包(也就是求出,哪两点之间是可达的) 最后用这个floyd求出的数组建出一个新图,在这个图上跑普通的最小路径 ...

  6. Bootstrap--响应式导航条布局

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  7. Session机制三(表单的重复提交)

    1.表单的重复提交的情况 在表单提交到一个servlet,而servlet又通过请求转发的方式响应了一个JSP页面,这个时候地址栏还保留这servlet的那个路径,在响应页面点击刷新. 在响应页面没有 ...

  8. 高仿360界面的实现(用纯XML和脚本实现)

    源码下载:360UI 本项目XML的桌面渲染依赖GQT开源项目(请感兴趣的朋友加入QQ讨论群:101189702,在群共享文件里下载GQT源码),以下是360界面实现的全部XML代码,所有的代码都在3 ...

  9. MySQL性能优化之char、varchar、text的区别

    参考来源:https://blog.csdn.net/brycegao321/article/details/78038272 在存储字符串时, 可以使用char.varchar或者text类型, 那 ...

  10. Revit二次开发示例:AutoUpdate

    在Revit打开文件时,修改文件信息.并记录状态,存到log文件中. #region Namespaces using System; using System.Collections.Generic ...