Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带查询字符串的.net 4.0路由_C#_.net 4.0_Url Rewriting_Url Routing_Webforms - Fatal编程技术网

C# 带查询字符串的.net 4.0路由

C# 带查询字符串的.net 4.0路由,c#,.net-4.0,url-rewriting,url-routing,webforms,C#,.net 4.0,Url Rewriting,Url Routing,Webforms,我知道有很多关于这个主题的文章和资源,但它们似乎都只展示了如何将url周围的category=shoes这样的查询字符串移动到不同的位置,比如products/{category} 我有以下查询字符串:profile.aspx?q=98c2b15f-90c3-4a7f-a33f-0e34b106877e 我试图实现一个RoutHandler来查询数据库以查找用户名并创建一个url,如mydomain.com/usersname 这就是我所尝试的(在我开始工作之前,一切都是硬编码的): 这是处理程

我知道有很多关于这个主题的文章和资源,但它们似乎都只展示了如何将url周围的category=shoes这样的查询字符串移动到不同的位置,比如products/{category}

我有以下查询字符串:profile.aspx?q=98c2b15f-90c3-4a7f-a33f-0e34b106877e

我试图实现一个RoutHandler来查询数据库以查找用户名并创建一个url,如mydomain.com/usersname

这就是我所尝试的(在我开始工作之前,一切都是硬编码的):

这是处理程序类:

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”查询字符串。在上面的设置中,它找不到它

我做错了什么?如何路由或重写url,使其美观并保留它,以便页面可以找到所需的查询字符串


任何帮助都会很好。提前感谢。

首先-如果您使用的是.net framework 4,则不需要创建任何处理程序,您可以直接使用MapPageRoute方法创建路由

回答你的问题- use可以在handler中使用如下所示的foreach循环,而不是专门查找“profile”

    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://msdn.microsoft.com/en-us/library/ie/cc668201.aspx

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