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

  准备

□ 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; }
    }
}
 

□ 模拟一个数据层服务类

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

MVC自定义路由01-为什么需要自定义路由

 

RentalProperty.cshtml

MVC自定义路由01-为什么需要自定义路由

 

Unit.cshtml

MVC自定义路由01-为什么需要自定义路由

 

  路由改进目标

■ 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

MVC自定义路由01-为什么需要自定义路由

 

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

MVC自定义路由01-为什么需要自定义路由

 

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

MVC自定义路由01-为什么需要自定义路由

 

□ 参考博文

Customizing Routes in ASP.NET MVC

相关文章:

  • 2021-08-12
  • 2021-07-20
  • 2022-12-23
  • 2021-08-15
  • 2021-06-22
  • 2022-02-26
猜你喜欢
  • 2022-01-07
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-10-20
相关资源
相似解决方案