.net 4.0 使用查询字符串进行路由

发布于 2025-01-01 21:54:29 字数 1167 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2025-01-08 21:54:29

首先 - 如果您使用 .net Framework 4,则无需创建任何处理程序,您可以直接使用 MapPageRoute 方法来创建路由。

回答你的问题——
use 可以在处理程序中使用 foreach 循环,如下所示,而不是专门寻找“配置文件”。

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

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

在您的路由页面中,您应该检查

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

您在 RouteHandler 构造函数中设置 VirtualPath,

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

请参阅这些链接以获取更多信息 -
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.

    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(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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文