我有以下顺序的路由规则:
routes.MapPageRoute("Corporate", "Shop/{Category}", "~/Shop.aspx");
routes.MapPageRoute("GiftType", "Shop/{Category}", "~/Shop.aspx");
routes.MapPageRoute("Occasion", "Shop/{Category}", "~/Shop.aspx");
routes.MapPageRoute("Discounted", "Shop/{Category}", "~/Shop.aspx");
routes.MapPageRoute("Featured", "Shop/{Featured}", "~/Shop.aspx");
我正在使用的链接:
<a href="~/Shop/Corporate" title="Corporate Gifts" runat="server">Corporate</a>
<a href="~/Shop/GiftType" title="Shop by Gift Type" runat="server">Gift Type</a>
<a href="~/Shop/Occasion" title="Shop by Occasion" runat="server">Occasion</a>
<a href="~/Shop/Discounted" title="Discounted" runat="server">Discounted</a>
<a href='<%# "~/Shop/"+Eval("Featured") %>' title='<%# Eval("ProductName") %>'
runat="server">
在目标页面的代码隐藏中,以下代码有效,我不知道为什么。对于“精选”的“真”案例值,是否正在执行 switch 语句?就像我没有根据占位符区分这些路线?
if (Page.RouteData.Values["Category"] != null)
{
string category = Page.RouteData.Values["Category"].ToString();
switch (category)
{
case ("Corporate"):
Response.Write("Corporate");
break;
case ("GiftType"):
Response.Write("GiftType");
break;
case ("Occasion"):
Response.Write("Occasion");
break;
case ("Discounted"):
Response.Write("Discounted");
break;
// Do not want the following to be picked up here for Category param
case ("True"):
Response.Write("Featured");
break;
}
}
我想在代码方面工作是这样的:
if (Page.RouteData.Values["Category"] != null)
{
string category = Page.RouteData.Values["Category"].ToString();
switch (category)
{
case ("Corporate"):
Response.Write("Corporate");
break;
case ("GiftType"):
Response.Write("GiftType");
break;
case ("Occasion"):
Response.Write("Occasion");
break;
case ("Discounted"):
Response.Write("Discounted");
break;
}
}
else if (Page.RouteData.Values["Featured"].ToString() == "True")
{
Response.Write("Featured");
}