MVC自定义路由01-为什么需要自定义路由
2014-03-11 01:13
232 查看
本篇体验自定义路由以及了解为什么需要自定义路由。
准备
□ViewModels
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{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;}
□模拟一个数据层服务类
RentalPropertiesController
视图
□All.csthml
□效果
http://localhost:1368/RentalProperties
http://localhost:1368/RentalProperties/长度
http://localhost:1368/RentalProperties/长度/Units/米
□参考博文
CustomizingRoutesinASP.NETMVC
准备
□ViewModels
usingSystem.Collections.Generic;
namespaceMvcApplication2.Models
{
//单位
publicclassUnit
{
publicintID{get;set;}
publicRentalPropertyRentalProperty{get;set;}
publicstringName{get;set;}
}
//属性
publicclassRentalProperty
{
publicintID{get;set;}
publicstringName{get;set;}
}
publicclassRentalPropertyTestData
{
publicintID{get;set;}
publicList<RentalProperty>RentalProperties{get;set;}
publicList<Unit>Units{get;set;}
}
}
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{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;}
□模拟一个数据层服务类
usingMvcApplication2.Models;
usingSystem.Collections.Generic;
namespaceMvcApplication2.Service
{
publicclassRentalService
{
publicRentalPropertyTestDataGetData()
{
List<RentalProperty>rps=newList<RentalProperty>();
RentalPropertyrp1=newRentalProperty(){ID=1,Name="长度"};
RentalPropertyrp2=newRentalProperty(){ID=2,Name="重量"};
rps.Add(rp1);
rps.Add(rp2);
List<Unit>units=newList<Unit>();
Unitunit1=newUnit(){ID=1,Name="米",RentalProperty=rp1};
Unitunit2=newUnit(){ID=2,Name="公斤",RentalProperty=rp2};
units.Add(unit1);
units.Add(unit2);
returnnewRentalPropertyTestData()
{
ID=1,
RentalProperties=rps,
Units=units
};
}
}
}
RentalPropertiesController
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Mvc;
usingMicrosoft.Ajax.Utilities;
usingMvcApplication2.Models;
usingMvcApplication2.Service;
namespaceMvcApplication2.Controllers
{
publicclassRentalPropertiesController:Controller
{
RentalPropertyTestData_data=newRentalPropertyTestData();
publicRentalPropertiesController()
{
RentalServices=newRentalService();
_data=s.GetData();
}
publicActionResultAll()
{
returnView(_data);
}
publicActionResultRentalProperty(stringrentalPropertyName)
{
varrentalProperty=_data.RentalProperties.Where(a=>a.Name==rentalPropertyName).FirstOrDefault();
returnView(rentalProperty);
}
publicActionResultUnit(stringrentalPropertyName,stringunitName)
{
varunit=_data.Units.Find(u=>u.Name==unitName&&u.RentalProperty.Name==rentalPropertyName);
returnView(unit);
}
}
}
视图
□All.csthml
展开usingSystem.Web.Mvc;
usingSystem.Web.Routing;
namespaceMvcApplication2
{
publicclassRouteConfig
{
publicstaticvoidRegisterRoutes(RouteCollectionroutes)
{
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}
);
}
}
}
□效果
□参考博文
相关文章推荐
- ASP.NET MVC 自定义路由中几个需要注意的小细节
- MVC 自定义异常错误页需要注意的地方
- 在ASP.NET MVC 3 中自定义AuthorizeAttribute时需要注意的页面缓存问题
- YbSoftwareFactory 代码生成插件【二十四】:MVC中实现动态自定义路由
- [asp.net mvc 奇淫巧技] 01 - 封装上下文 - 在View中获取自定义的上下文
- [ASP.NET MVC2 系列] ASP.NET MVC 之如何创建自定义路由约束
- Android安全输入设计与思考,android设计思考 为什么使用安全键盘? 安全的输入 各大公司的安全键盘设计 开始自定义安全键盘 安全键盘还需要注意的
- ASP.NET MVC 之创建自定义路由约束
- Item 50:为什么需要自定义new和delete?
- MVC的Partial ,自定义路由,认证和授权
- [asp.net mvc 奇淫巧技] 01 - 封装上下文 - 在View中获取自定义的上下文
- 创建自定义路由处理程序(Creating a Custom Route Handler) | 定制路由系统| 高级路由特性 |精通ASP-NET-MVC-5-弗瑞曼
- ASP.NET MVC 自定义路由
- MVC路由自定义后缀名地址
- 为ASP.NET MVC应用添加自定义路由
- 为ASP.NET MVC应用添加自定义路由
- 使用HashSet 存储自定义对象,为什么需要重写hashCode()和equals()?
- 关于自定义异常中为什么带参构造器需要显示调用父类异常的带参构造器
- [翻译:ASP.NET MVC 教程]创建自定义路由
- 在ASP.NET MVC 3 中自定义AuthorizeAttribute时需要注意的页面缓存问题