您的位置:首页 > 其它

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

2014-03-11 01:13 232 查看
本篇体验自定义路由以及了解为什么需要自定义路由。

准备

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

□效果
http://localhost:1368/RentalProperties



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



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




□参考博文

CustomizingRoutesinASP.NETMVC
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: