简体   繁体   中英

Can I create an ASP MVC route that only accepts uppercase in the URL?

I have the following route:

context.MapRoute(
    "content",
    "{page}/{title}",
    new { controller = "Server", action = "Index" },
    new { page = @"^[AFL][0-9A-Z]{4}$" }
);

This route is used for pages such as:

/A1234
/F6789
/L0123

However it also catches: /Admin which is something I do not want.

I put a temporary solution in place that looks like this:

context.MapRoute(
    "content",
    "{page}/{title}",
    new { controller = "Server", action = "Index" },
    new { page = @"^[AFL][0-9][0-9A-Z]{3}$" }
);

This only works because right now all my pages have a 0 in the second digit.

Is there a way I could configure my route to accept A,F or L followed by 4 upper case characters but have it not catch "dmin" ?

Not sure if this is the case but I am thinking the regular expression should not accept "dmin" as it's in lower case and I only specify AZ. However when used as an MVC route it does take "dmin". Does anyone know if ASP MVC internally converts this to all uppercase?

Solution 1: Custom route constraint class

Default route processing ignores case (see code below) when matching URLs that's why Admin in your case matches as well. All you should do is to write a custom route constraint class that implements IRouteConstraint interface and implement Match method appropriately to be case sensitive.

Here's a tutorial to get you started

Solution 2: Custom Route class

If you look at how default Route class processes constraints, this is the code:

protected virtual bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
    IRouteConstraint routeConstraint = constraint as IRouteConstraint;

    // checks custom constraint class instances
    if (routeConstraint != null)
    {
        return routeConstraint.Match(httpContext, this, parameterName, values, routeDirection);
    }

    // No? Ok constraint provided as regular expression string then?
    string text = constraint as string;
    if (text == null)
    {
        throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, SR.GetString("Route_ValidationMustBeStringOrCustomConstraint"), new object[]
        {
            parameterName,
            this.Url
        }));
    }
    object value;
    values.TryGetValue(parameterName, out value);
    string input = Convert.ToString(value, CultureInfo.InvariantCulture);
    string pattern = "^(" + text + ")$";

    // LOOK AT THIS LINE
    return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.CultureInvariant);
}

The last line actually matches provided regular expression route constraints. As you can see it ignores case. So the second possible solution is to write a new Route class that inherits from this default Route class and override ProcessConstraint method to not ignore case. Everything else can then stay the same.

Another alternative is a Regex like this:

@"^(?!Admin)[AFL][0-9][0-9A-Z]{3}$"

Which will exclude Admin as a possible match before trying the rest of the regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM