简体   繁体   English

.net 4.0使用QueryStrings进行路由

[英].net 4.0 Routing with QueryStrings

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} 我意识到这个主题上有大量的文章和资源,但似乎只是展示了如何将类似=鞋类的查询字符串移动到不同的地方,例如此产品/ {category}

Well i have the following querystring: profile.aspx?q=98c2b15f-90c3-4a7f-a33f-0e34b106877e 好吧,我有以下查询字符串: 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 我试图实现一个RoutHandler来查询数据库以查找用户名并创建一个像mydomain.com/usersname的URL

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. Profile.aspx实际上查找“q”查询字符串。 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. 提前致谢。

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. 第一件事 - 如果你使用.net框架4你不需要创建任何处理程序,你可以直接使用MapPageRoute方法来路由。

Answer to your question- use can use foreach loop like below in handler rather than looking for "profile" specifically. 回答你的问题 - 使用可以在处理程序中使用类似下面的foreach循环,而不是专门查找“profile”。

    foreach (var urlParm in requestContext.RouteData.Values)
         requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;

    return BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler

and in your routed page you should check for 在您的路由页面中,您应该检查

    string userName = this.Context.Items["profile"].ToString();       // "userName" and is set as route parameters in Global.asax

You set VirtualPath in RouteHandler constructor 您在RouteHandler构造函数中设置VirtualPath

 RouteHandler(string virPath)
    {
        this.VirtualPath = virPath;
    }

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 有关更多信息,请参阅这些链接 - http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET-4-0 http://msdn.microsoft.com/en-us/library/即/ cc668201.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM