.net 4.0 使用查询字符串进行路由
我意识到有大量关于这个主题的文章和资源,但所有似乎都只展示了如何将 url 周围的 category=shoes 之类的查询字符串移动到不同的位置,例如 >products/{category}
嗯,我有以下查询字符串: profile.aspx?q=98c2b15f-90c3-4a7f-a33f-0e34b106877e
我试图实现一个 RoutHandler 来查询数据库以查找用户名并创建一个像 mydomain.com/ 这样的网址用户名
这是我尝试过的(现在一切都是硬编码的,直到我让它工作):
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;
}
Profile.aspx 实际上查找“q”查询字符串。通过上述设置,它没有找到它。
我做错了什么?我如何路由或重写 url,使其美观 + 保留它,以便页面可以找到它需要的查询字符串?
任何帮助都会很棒。提前致谢。
I realize there are a ton of articles and resources out there on this subject, but all seem to only show how to move a querystring like category=shoes around the url to a differently place, like this products/{category}
Well i have the following querystring: profile.aspx?q=98c2b15f-90c3-4a7f-a33f-0e34b106877e
I was trying to implement a RoutHandler to query the DB to find the users name and create a url like mydomain.com/usersname
This is what i tried (everything is hard coded right now till i get it working):
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()));
}
And this is the handler class:
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;
}
Profile.aspx actually looks for the "q" querystring. And with the above setup, it does not find it.
What am i doing wrong? How do i route or rewrite the url so that it is pretty + keep it so the page can find the querystrings it needs?
Any help would be great. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先 - 如果您使用 .net Framework 4,则无需创建任何处理程序,您可以直接使用 MapPageRoute 方法来创建路由。
回答你的问题——
use 可以在处理程序中使用 foreach 循环,如下所示,而不是专门寻找“配置文件”。
在您的路由页面中,您应该检查
您在 RouteHandler 构造函数中设置 VirtualPath,
请参阅这些链接以获取更多信息 -
http://www.codeproject.com/Articles /77199/URL-Routing-with-ASP-NET-4-0
http://msdn.microsoft.com/en-us/library/ie/cc668201.aspx
First thing - If you are using .net framework 4 you dont need to create any handler, you can directly use MapPageRoute method to a route.
Answer to your question-
use can use foreach loop like below in handler rather than looking for "profile" specifically.
and in your routed page you should check for
You set VirtualPath in RouteHandler constructor
see these links for more information-
http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET-4-0
http://msdn.microsoft.com/en-us/library/ie/cc668201.aspx