Orchard CMS中的自定义路径约束无效

时间:2016-11-24 04:42:17

标签: c# asp.net-mvc routes orchardcms

我正在尝试在Orchard CMS中设置自定义路由,目的是在URL中捕获“用户”名称,以便他们可以在前端查看他们的个人资料;目前他们的个人资料可通过以下方式访问:

http://localhost:30321/OrchardLocal/Orc/Profile?username=Terry

我希望复制的是几乎所有网站所做的事情,也就是说,只需通过以下方式访问此页面:

http://localhost:30321/OrchardLocal/Orc/Profile/Terry

我的代码是:

using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard;
using Orchard.Mvc.Routes;
using Orchard.Security;

namespace PP.Orc.Routes
{
public class ProfileRoutes : IRouteProvider {
    private static readonly IMembershipService _membershipService;
    private static readonly IOrchardServices _orchardServices;

    public void GetRoutes(ICollection<RouteDescriptor> routes) {
        foreach (var routeDescriptor in GetRoutes())
            routes.Add(routeDescriptor);
    }

    public IEnumerable<RouteDescriptor> GetRoutes() {
        return new[] {

            new RouteDescriptor {
                Priority = 5,
                Route = new Route(
                    "Profile/{username}",
                    new RouteValueDictionary {
                        {"area", "PP.Orc"},
                        {"controller", "Profile"},
                        {"action", "Index"},
                        //{"username", new UserNameConstraint() }//UrlParameter.Optional }
                    },
                    new RouteValueDictionary {
                        {"username",  new UserNameConstraint()}
                    },
                    new RouteValueDictionary {
                        {"area", "PP.Orc"}
                    },
                    new MvcRouteHandler())

            }
        };
    }

    public class UserNameConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            // Get the username from the url
            var username = values["username"].ToString().ToLower();
            // Check for a match (assumes case insensitive)
            IUser user = _membershipService.GetUser(username);

            if (user == null || !_orchardServices.Authorizer.Authorize(Permissions.ViewProfiles, user, null))
            {
                return false;
            }

            return true;
        }
    }
}
}

但是,在尝试所需的网址时 - 我收到的只是:

'/ OrchardLocal'应用程序中的

服务器错误。 无法找到资源。

显然,我错过了一些我不知道的元素,或者没有完全掌握如何正确实现路线?

更新

所以我发现了什么有用 - 但我不知道为什么重要 - 以及为什么其他描述符起作用,就像我原来的那样,并继续这样做?

基本上,添加 PP /个人资料/ {用户名} 可以使其正常运行......

然而,为了简洁起见,我最初忽略了另外两个描述符(这两个描述符都有效):

           new RouteDescriptor {
               Priority = 7,
               Route = new Route(
                   "Profile/Register",
                   new RouteValueDictionary {
                       {"area", "PP.Orc"},
                       {"controller", "Profile"},
                       {"action", "Register"}
                   },
                   new RouteValueDictionary (),
                   new RouteValueDictionary {
                       {"area", "PP.Orc"}
                   },
                   new MvcRouteHandler())
            },
            new RouteDescriptor {
                Priority = 6,
                Route = new Route(
                    "Profile/Edit",
                    new RouteValueDictionary {
                        {"area", "PP.Orc"},
                        {"controller", "Profile"},
                        {"action", "Edit"}
                    },
                    new RouteValueDictionary (),
                    new RouteValueDictionary {
                        {"area", "PP.Orc"}
                    },
                    new MvcRouteHandler())

            },

...所以我不知道为什么它们在没有'PP'的情况下工作,但USERNAME描述符不起作用?

1 个答案:

答案 0 :(得分:0)

如果你把Profile / {username}与?与第一种情况一样,尝试放置Profile / username