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

    □ 模拟一个数据层服务类

    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

    展开

    □ RentalProperty.cshtml

    展开

    □ Unit.cshtml

    展开

      效果

    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

      添加自定义路由规则

    展开

    □ 效果

    http://localhost:1368/RentalProperties

    后所有

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

    后属性

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

    后单位

    □ 参考博文

    Customizing Routes in ASP.NET MVC

  • 相关阅读:
    关于使用stanfordcorenlp一直运行不报错的解决方法
    小程序项目报错
    小程序项目学习笔记
    如何将知网下载的caj文件转换为pdf文件
    干眼症治疗方法
    事务基础
    Android的四大组件
    异步任务AsyncTask使用解析
    Android Service的生命周期
    2016 校招, Android 开发,一个本科应届的坎坷求职之路(转)
  • 原文地址:https://www.cnblogs.com/darrenji/p/3593085.html
Copyright © 2020-2023  润新知