我意識到這個主題上有大量的文章和資源,但所有似乎只顯示如何移動一個查詢字符串周圍的url到一個不同的地方,如category = shoes,這樣產品/ {類別}.net 4.0使用QueryStrings路由
嗯,我有以下查詢字符串:profile.aspx q = 98c2b15f-90c3-4a7f-A33F-0e34b106877e
我是想實現一個RoutHandler查詢? DB找到用戶名並創建一個url,如mydomain.com/ usersname
這是我嘗試(一切都硬編碼現在直到我得到它的工作):
void Application_Start(object sender, EventArgs e)
{
RegisterRoute(System.Web.Routing.RouteTable.Routes);
}
void RegisterRoute(System.Web.Routing.RouteCollection routes)
{
routes.Add("Profiles", new System.Web.Routing.Route("profile/{profile}", new RouteHandler()));
}
這是處理類:
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string username = requestContext.RouteData.Values["profile"] as string;
HttpContext.Current.Items["q"] = "98c2b15f-90c3-4a7f-a33f-0e34b106877e";
return BuildManager.CreateInstanceFromVirtualPath("~/pub/profile.aspx", typeof(Page)) as Page;
}
檔案。 aspx實際上查找「q」查詢字符串。通過上述設置,它不會找到它。
我在做什麼錯了?我如何路由或重寫網址,以便它很漂亮+保持它,以便頁面可以找到它需要的查詢字符串?
任何幫助將是偉大的。提前致謝。