开发者

How to make IRouteConstraint filter route

I wrote a custom route constraint, but its filter just doesn't get开发者_Python百科 recognized. Does anyone have an example working use of IRouteConstraint ?

Also, note to developers: I get double display of the form on my android. Something must be wrong with the partial rendering?


Here's a simple constraint that looks up an article slug in a fictional repository:

public class SlugRouteConstraint : IRouteConstraint
{
    private readonly ISlugRepository slugRepository = new SlugRepository();

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (!values.ContainsKey(parameterName))
            return false;

        var slug = (string)values[parameterName];

        return slugRepository.Exists(slug);
    }
}

You could wire up the constraint like this:

routes.MapRoute("Slugs", "{slug}",
    new { controller = "Articles", action = "View" },
    new { slug = new SlugConstraint() });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜