如何更改带有名称的URL ID,但我想传递ID,它显示URL中与ID对应的Name

时间:2018-08-14 10:56:19

标签: c# asp.net

我中继器项目绑定在此i传递ID中创建一个超链接URL,但是当用户单击它时,它是该页面上的传递ID,但URL中显示了它的名称。 像这样的例子:

  

localhost:50636 / Products / Categories / Categories.aspx /?mc = 1&cs = 3&mid = 1

现在,客户希望看起来像这样:

  

localhost:50636 /产品/类别/ xyzname / abcfv / ufo

我正在尝试,但没有成功,请任何人可以帮助

protected void Rep_BindMenuItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        tblMenuCategory dataItem = (tblMenuCategory)e.Item.DataItem;
        HyperLink Menu = (HyperLink)e.Item.FindControl("HyperLinkMenu");
        Menu.Text = dataItem.MenuCategory;
        int MenuId = dataItem.MenuCategoryID;
        Repeater repeatercategory = (Repeater)e.Item.FindControl("RepBindMenuCategItem");
        repeatercategory.DataSource = BLCategory.CategoryLoadByMenuId(MenuId);
        repeatercategory.DataBind();

        //Menu.NavigateUrl = string.Concat("~/Products/Categories/Categories.aspx/?mc=", MenuId.ToString() + "&cs=" + 0 + "&mid=" + MenuId.ToString());

        Response.RedirectToRoute("Selected Menu", new { Menuname = Menu.Text, mc = MenuId.ToString(), cs = 0, mid = MenuId.ToString() });
    }
}

我在Global.asax中传递了喜欢的值

protected void Application_Start(object sender, EventArgs e)
{
   RegisterRoutes(RouteTable.Routes);
}

之后我通过

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Selected Menu", "{Menuname}/{mc}/{cs}/{mid}", "~/Products/Categories/Categories.aspx");
}

1 个答案:

答案 0 :(得分:0)

在这里,我想您已经在数据库或任何其他资源中显示了菜单,并且菜单中有下面的字段,如类

public class MyMenu
{
    public int MenuId { get; set; }
    public string MenuText { get; set; }
    public string MC { get; set; }
    public string CS { get; set; }
    public string MID { get; set; }
}

然后,您必须创建一个方法,该方法将为您提供带有数据库或其他任何资源(如

)中的ID的特定菜单
public MyMenu GetMenuById(int MenuId)
{
    //Database or repository call to get your menu data with menuId
}

您将像下面那样使用上面的方法

protected void Rep_BindMenuItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        tblMenuCategory dataItem = (tblMenuCategory)e.Item.DataItem;
        **HyperLink Menu * * = (HyperLink)e.Item.FindControl("HyperLinkMenu");
        Menu.Text = dataItem.MenuCategory;
        int MenuId = dataItem.MenuCategoryID;

        //Get menu by calling below method with menuid
        MyMenu myMenu = GetMenuById(MenuId);

        Repeater repeatercategory = (Repeater)e.Item.FindControl("RepBindMenuCategItem");
        repeatercategory.DataSource = BLCategory.CategoryLoadByMenuId(MenuId);
        repeatercategory.DataBind();

        //Bind those menu data to below parameters values 
        Response.RedirectToRoute("Selected Menu", new { Menuname = myMenu.MenuText, mc = myMenu.MC, cs = myMenu.CS, mid = myMenu.MID });
    }
}

尝试一次,可能对您有帮助。