How to Hide an ID from an URL
net 4 and c# with Routing.
I would like to know how to pass a value in a DataToken from GetRouteUrl (or another ways).
My aim is to crete a Url like "mysite.com/category-title"
and passing (hiding from the URL the categoryId
) but in the destination page I would like retrieve the hidden categoryId
for filtering.
At the moment I try to accomplish this using DataTokens, without result.
My questions:
- How to hide an Id in the Url generated by Routing and passing hidden an Id?
- Is it my approach the right way (using DataTokens)? Any better idea (please provide me a sample of code)
Thanks for your time on this.
<asp:HyperLink runat="server" Text='<%# Eval("Title") %>' NavigateUrl='<%# GetRouteUrl( new { Title = Eval("Title") }) %>'></asp:HyperLink>
public 开发者_如何学Pythonstatic void RegisterRoutes(RouteCollection routes)
{
// Ex: mysite.com/category-title/
routes.MapRouteCategoryView("myRouteName", "{Title}");
}
I would
public static void MapRouteCategoryView(this RouteCollection routes, string name, string url)
{
// Page Handler map filter to physical path.
PageRouteHandler handler = new PageRouteHandler("~/Cms/FrontEndCms/CategoryView.aspx");
// Create the Route.
CustomRoute myRoute = new CustomRoute(url, handler);
// Setting the Route.
//myRoute.Constraints = new RouteValueDictionary { { "CategoryId", @"\d+" } }; // Only valid Int for Id.
myRoute.DataTokens = new RouteValueDictionary { { "RouteName", name } };
// Add the Route to RouteCollection.
routes.Add(myRoute);
}
You could try just encrypting the categoryID in the QueryString.
If you are using the latest version of IIS you can use the URL Rewrite module.
I have usually done something similar to this by using the URL to get the category (or page in my case) by the querystring value the route maps to and then search the database by the route (or path).
Hope this helps.
Craig
精彩评论