Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:01:23+00:00 2026-06-09T18:01:23+00:00

When adding custom routing constraints to my route parameters I am finding that it

  • 0

When adding custom routing constraints to my route parameters I am finding that it is breaking the Url.Action method I use to build my links. If the route constraint is simply a regular expression then the Url.Action method continues to recognize the parameter, however if it is a custom constraint which I define, Url.Action method gives my parameter as a request parameter.

Here is my route definition:

routes.MapRoute(
            "Event",
            "Events/{strDate}",
            new { controller = "Events", action = "Index", strDate = DateTime.Today.ToString("yyyy-MM-dd") },
            new { strDate = new IsValidDateConstraint() },
            new[] { "MyProject.Controllers" }
        );

routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "MyProject.Controllers" }
        );

The IsValidDateConstraint class inherits from IRouteConstraint and returns true or false if the strDate parameter parses correctly to a DateTime object:

public class IsValidDateConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (routeDirection == RouteDirection.IncomingRequest)
        {
            DateTime dt = new DateTime();

            if (DateTime.TryParse(values["strDate"].ToString(), out dt))
                return true;
        }

        return false;
    }
}

Using the Url.Action method to build URL’s:

@Url.Action("Index", "Events", new { strDate = ViewBag.CurrentDate.AddDays(1).ToString("yyyy-MM-dd") })

The resulting link is: /Events?strDate=2012-08-15

Everything routes correctly if I type in /Events/2012-08-15, it’s just that the Url.Action method is not recognizing that strDate is a parameter defined in my route only when I apply my custom routing constraint. If I comment out the custom routing constraint then the Url.Action method maps the URL correctly.

Any ideas on why the Url.Action is not recognizing my route parameter when I have a custom route constraint defined?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:01:23+00:00Added an answer on June 9, 2026 at 6:01 pm

    You haven’t shown how your IsValidDateConstraint looks like but make sure you are doing a culture invariant parsing for the yyyy-MM-dd format:

    public class IsValidDateConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            DateTime date;
            return DateTime.TryParseExact(
                values[parameterName] as string, 
                "yyyy-MM-dd", 
                CultureInfo.InvariantCulture, 
                DateTimeStyles.None, 
                out date
            );
        }
    }
    

    Also make sure that this route is placed before the default route:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            "Event",
            "Events/{strDate}",
            new { controller = "Events", action = "Index", strDate = DateTime.Today.ToString("yyyy-MM-dd") },
            new { strDate = new IsValidDateConstraint() },
            new[] { "MyProject.Controllers" }
        );
    
        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
    

    also DateTime.Parse(ViewBag.CurrentDate.ToString()) looks a wee-bit of a WTFkish code. If ViewBag.CurrentDate is already a DateTime you could directly write:

    @Url.Action(
        "Index", 
        "Events", 
        new { 
            strDate = ViewBag.CurrentDate.AddDays(1).ToString("yyyy-MM-dd") 
        }
    )
    

    Obviously a much better solution is to use view models:

    @Url.Action(
        "Index", 
        "Events", 
        new { 
            strDate = Model.CurrentDate.AddDays(1).ToString("yyyy-MM-dd") 
        }
    )
    

    UPDATE:

    Now that you have shown your code the problem comes from the if condition you have put in your constraint:

    if (routeDirection == RouteDirection.IncomingRequest)
    

    When using the Url.Action helper this condition is never satisfied. Only when resolving an incoming url. So you will have to remove it if you want this constraint to work with url helpers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that is adding custom objects to an array in a
I am adding custom button to my UITableViewCell . In the action of that
I'm adding ASP.NET routing to an older webforms app. I'm using a custom HttpHandler
By adding a custom handler to the SiteMapResolve event, I can update sitemap url's
Is there a way of adding custom assertions to the NodeUnit test object that
Is there any way of adding custom keyboard shortcuts to a specific page? In
How is working with Magento when it comes to adding custom functionality? From an
I've a question concerning handling touch events for CustomView .I'm adding custom view's dynamically
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane.
I'm adding a custom context menu item to documents (and not folders) in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.