URL路由在ASP.net,需要从URL得到的参数 [英] URL Routing on ASP.net, need to get a parameter from the url

查看:181
本文介绍了URL路由在ASP.net,需要从URL得到的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发的asp.net应用程序组功能。 我想给用户直接访问群体,所以我想的网址是

I am developing group feature on asp.net application. I want to give users direct access to groups, so I want the url to be

www.<domain>.com/<groupname>

我实现URL路由对于这种情况,但我想通过组名的以ASP页面参数的问题,我该怎么办呢?

I implemented URL Routing for this case, but the problem that I want to pass the group name an to asp page as parameter, how can I do that?

实际路径是〜/公共/ ViewGroup中组=&LT;组名&GT; ,如何得到这个组的名称,并将其添加到虚拟路径

the actual path is "~/Public/ViewGroup?group=<groupname> , how to get this group name and add it to the virtual path?

感谢

推荐答案

简单的回答是使用:

routes.MapPageRoute(
   "groupname",
   "{group}",
   "~/public/viewgroup"
);

然后,而不是(或者以及)使用查询字符串来提取值〜/公/ ViewGroup中 code,你会改为提取的RouteData 的组名如下:

And then instead of (or as well as) using querystring to extract the value in ~/public/viewgroup code, you would instead extract the groupname from RouteData as follows.

ControllerContext.RouteData.Values["groupname"];

另一种选择是使用IIS重写模块。

The other option is use the IIS rewrite module.

<rewrite>
    <rules>
        <rule name="groupname">
            <match url="^([^/]*)$" />
            <action type="Rewrite" url="public/viewgroup?group={R:1}" />
        </rule>
    </rules>
</rewrite>

如果你真正的必须的传递值作为新的查询字符串的价值,并希望使用路由,那么事情变得棘手。实际上,你必须定义一个自定义处理程序和重写的路径,以添加路由值到查询字符串

If you really must pass the value as a new querystring value, and want to use Routing, then things get tricky. You actually have to define a custom handler and rewrite the path in order to append the routing values to the querystring.

public class RouteWithQueryHandler : PageRouteHandler
{
    public RouteWithQueryHandler(string virtualPath, bool checkPhysicalUrlAccess)
        : base(virtualPath, checkPhysicalUrlAccess)
    {
    }

    public RouteWithQueryHandler(string virtualPath)
        :base(virtualPath)
    {
    }

    public override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        var request = requestContext.HttpContext.Request;
        var query  = HttpUtility.ParseQueryString(request.Url.Query);
        foreach (var keyPair in requestContext.RouteData.Values)
        {
            query[HttpUtility.UrlEncode(keyPair.Key)] = HttpUtility.UrlEncode(
                                               Convert.ToString(keyPair.Value));
        }
        var qs = string.Join("&", query);
        requestContext.HttpContext.RewritePath(
                             requestContext.HttpContext.Request.Path, null, qs);
        return base.GetHttpHandler(requestContext);
    }
}

这可以注册如下:

routes.Add("groupname", new Route("{groupname}/products.aspx",     
           new RouteWithQueryHandler("~/products.aspx", true)));

这是一个相当大量的工作,以避免刚拉出来的价值的路由数据。

It's quite a lot of work to avoid just pulling the value out to the Routing data.

这篇关于URL路由在ASP.net,需要从URL得到的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆