如何处理asp.net核心中的seo友好路由,如asp.net mvc?

时间:2017-11-10 09:51:35

标签: c# asp.net asp.net-mvc asp.net-core routing

我在asp.net mvc中有seo友好路由,它基本上与id一起广告标题但在被叫时它将id传递给action但在url中显示title + id。

这是代码。

 routes.Add("ClinicDetails", new SeoFriendlyRoute("veterinary-clinics/{id}/details",
                new RouteValueDictionary(new { controller = "clinics", action = "details" }),
                new MvcRouteHandler()));


public class SeoFriendlyRoute : Route
    {
        public SeoFriendlyRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler)
        {
        }
        public SeoFriendlyRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler, IEnumerable namespaces) : base(url, defaults, routeHandler)
        {
            DataTokens = defaults;
            defaults["Namespaces"] = namespaces;
        }
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var routeData = base.GetRouteData(httpContext);
            if (routeData == null) return null;
            if (routeData.Values.ContainsKey("id"))
                routeData.Values["id"] = GetIdValue(routeData.Values["id"]);
            return routeData;
        }
        private object GetIdValue(object id)
        {
            if (id == null) return null;
            var idValue = id.ToString();
            var regex = new Regex(@"^[\w|\W]+-(?<id>\d+).*$");
            var match = regex.Match(idValue);
            return match.Success ? match.Groups["id"].Value : id;
        }
    }

我如何在asp.net核心中获得相同的功能。

1 个答案:

答案 0 :(得分:0)

这是解决方案,我自己找到了

    public static class RouteExtensions
        {
            public static IRouteBuilder MapSeoFriendlyRoute(this IRouteBuilder routeBuilder, string name,
                string template, object defaults)
            {
                var inlineConstraintResolver = (IInlineConstraintResolver)routeBuilder
                    .ServiceProvider
                    .GetService(typeof(IInlineConstraintResolver));

                var defaultsDictionary = new RouteValueDictionary(defaults);
                routeBuilder.Routes.Add(new SeoFriendlyRoute(routeBuilder.DefaultHandler,
                name,
                template,
                defaultsDictionary,
                constraints: null,
                dataTokens: null,
                inlineConstraintResolver: inlineConstraintResolver));
                return routeBuilder;
            }
        }


        public class SeoFriendlyRoute : Route
        {
            public SeoFriendlyRoute(IRouter target, string routeTemplate, IInlineConstraintResolver inlineConstraintResolver) : base(target, routeTemplate, inlineConstraintResolver)
            {
            }

            public SeoFriendlyRoute(IRouter target, string routeTemplate, RouteValueDictionary defaults, IDictionary<string, object> constraints, RouteValueDictionary dataTokens, IInlineConstraintResolver inlineConstraintResolver) : base(target, routeTemplate, defaults, constraints, dataTokens, inlineConstraintResolver)
            {
            }

            public SeoFriendlyRoute(IRouter target, string routeName, string routeTemplate, RouteValueDictionary defaults, IDictionary<string, object> constraints, RouteValueDictionary dataTokens, IInlineConstraintResolver inlineConstraintResolver) : base(target, routeName, routeTemplate, defaults, constraints, dataTokens, inlineConstraintResolver)
            {
            }

            protected override Task OnRouteMatched(RouteContext context)
            {
                var routeData = context.RouteData;
                if (routeData != null)
                {
                    if (routeData.Values.ContainsKey("id"))
                        routeData.Values["id"] = GetIdValue(routeData.Values["id"]);
                }
                return base.OnRouteMatched(context);
            }

            public override Task RouteAsync(RouteContext context)
            {
                return base.RouteAsync(context);
            }

            private object GetIdValue(object id)
            {
                if (id == null) return null;
                var idValue = id.ToString();
                var regex = new Regex(@"^[\w|\W]+-(?<id>\d+).*$");
                var match = regex.Match(idValue);
                return match.Success ? match.Groups["id"].Value : id;
            }
        }

routes.MapSeoFriendlyRoute("AdDetails", "ads/{id}/details",
                defaults: new { controller = "ads", action = "details" });