MapPageRoute中的通配符 [英] Wildcard in MapPageRoute

查看:114
本文介绍了MapPageRoute中的通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个webforms项目,在这里我使用System.Web.Routing.RouteCollection.MapPageRoute重写URL,但是我对一些动态URL遇到了问题.我的网址可能看起来像这样;

I have a webforms-project where I use System.Web.Routing.RouteCollection.MapPageRoute to rewrite URLs but I have a problem with a few dynamic URLs. My URL could look like this;

/folder/city-1-2-something.aspx

和用于此的MapPageRoute看起来像这样

and the MapPageRoute for this looks like this

routeCollection.MapPageRoute("CompanyCity", "folder/city-{id}-{pid}-{title}.aspx", "~/mypage.aspx");

但是我已经意识到某些URL可能看起来像这样

But I have realized that some URLs could look like this

/folder/city-2-2-something-something.aspx
/folder/city-2-2-something-something-something.aspx
/folder/city-2-2-something-something-something-something.aspx

而我的路由无法正确处理这些结果-第一个示例将以id = 2-2和pid = something而不是id = 2和pid = 2的结果结束.

and these are not cought correctly by my routing - the first example will end up with the results id = 2-2 and pid = something instead of id = 2 and pid = 2.

{title}并不重要-仅使用{id}和{pid}.我有一些通往特定文件夹的类似路线,因此,据我所知,我无法使用全部捕获功能.但是我该如何解决这个问题?

The {title} is not important - only {id} and {pid} are used. I have several similar routes to specific folders, so as far as I can se I cannot use a catch all. But how can I fix this issue?

推荐答案

下面的简单RouteConfig包含一个与您所需要的完全匹配的TestRoute.仅此而已,因此从某种意义上讲它是很糟糕的代码.

The simple RouteConfig below contains a TestRoute that matches exactly what you need. And nothing more,so it is in a sense quite bad code.

但是我们的想法是,现在可以使用很容易满足您需求的正则表达式.(命名组"id" (?< id> \ d)和"pid" (?< pid> \ d)仅与数字(\ d )为什么它们只会匹配到下一个破折号.)

But the idea is that it is now possible to use regular expressions which can quite easily match your needs. (The named groups "id" (?<id>\d) and "pid" (?<pid>\d) only matches digits (\d) why they will only match until next dash.)

希望它会有所启发.

using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace InfosoftConnectSandbox
{
    public class RouteConfig
    {
        class TestRoute : RouteBase
        {
            Regex re = new Regex(@"folder/city-(?<pid>\d)-(?<id>\d)-.*");

            public override RouteData GetRouteData(HttpContextBase httpContext)
            {
                var data = new RouteData();

                var url = httpContext.Request.Url.ToString();

                if (!re.IsMatch(url))
                {
                    return null;
                }

                foreach (Match m in re.Matches(url))
                {
                    data.Values["pid"] = m.Groups["pid"].Value;
                    data.Values["id"] = m.Groups["id"].Value;
                }

                data.RouteHandler = new PageRouteHandler("~/mypage.aspx");

                return data;
            }

            public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
            {
                return new VirtualPathData(this, "~/mypage.aspx");
            }
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.Add(new TestRoute());

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

这篇关于MapPageRoute中的通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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