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 6331413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:03:38+00:00 2026-05-24T18:03:38+00:00

I have a route defined like this: routes.MapRoute(Date, Date/{year}/{month}/{day}, new { controller = Date,

  • 0

I have a route defined like this:

 routes.MapRoute("Date", "Date/{year}/{month}/{day}", 
    new { controller = "Date", action = "Index", year = UrlParameter.Optional,
         month = UrlParameter.Optional, day = UrlParameter.Optional });

So it has 3 optional parameters, year, month and day. It works fine in routing a GET request, all the following work fine:

http://myhost/myapp/Date
http://myhost/myapp/Date/2011
http://myhost/myapp/Date/2011/8
http://myhost/myapp/Date/2011/8/17

Generating links using RouteUrl works in all cases except one.

For example, the case where I want a link including the year and a month works fine. I use the following (simplified) code in my view:

 string linkUrl = Url.RouteUrl("Date",
     new { controller = "Date", year = 2011, month = 8 },
     Request.Url.Scheme);

But the case where only the year is defined does not work and returns null, the code is:

string linkUrl = Url.RouteUrl("Date",
    new { controller = "Date", year = 2011 },
    Request.Url.Scheme);

So it all looks correct to me. Any clue what I’m doing wrong, or how to debug this further.

  • 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-05-24T18:03:53+00:00Added an answer on May 24, 2026 at 6:03 pm

    Ok, it looks like this is a known ‘issue’ introduced in MVC3, but due to an underlying issue in routing in .NET4, so cannot be quickly fixed by the MVC team. Although silently returning null is clearly a bug, its unclear whether the route I originally created should be allowed to work the way I wanted it too (see below).

    I found this SO Question which asks for people to replicate a similar issue with two optional parameters, and an answer refers to this article too, which has a workaround.

    I resolved this as per the workaround by making multiple routes, with only one optional parameter in each.

      routes.MapRoute(
        "Date-ByDay", // Route name
        "Date/{year}/{month}/{day}", // URL with parameters
        new { controller = "Date", action = "Index" } // Parameter defaults
      );
    
      routes.MapRoute(
        "Date-ByMonth", // Route name
        "Date/{year}/{month}", // URL with parameters
        new { controller = "Date", action = "Index", month = UrlParameter.Optional } // Parameter defaults
      );
    
      routes.MapRoute(
        "Date-ByYear", // Route name
        "Date/{year}", // URL with parameters
        new { controller = "Date", action = "Index", year = UrlParameter.Optional } // Parameter defaults
      );
    

    It is arguable whether my original approach should work anyway, for example what if you miss out the optional ‘month’ parameter, and should the original route generate a URL of /2011/18? The ‘workaround’ whilst ugly, certainly is much more explicit about the behavior you want. I would argue that the expected behavior of my original route is clear (to me) and that I would have expected an exception if I had asked for a route with a year and a day, missing out the month – but I can see that this may not be everyones point of view. However this did work in MVC2.

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

Sidebar

Related Questions

I have my default route defined like this routes.MapRoute( Default, // Route name {controller}/{action}/{id},
I have the following route defined routes.MapRoute( ItemName, {controller}/{action}/{projectName}/{name}, new { controller = Home,
I have a route: routes.MapRoute( Company, {id}/{name}.aspx, new { controller = Company, action =
I have these routes defined: routes.MapRoute(CategoryList_CountryLanguage, Categories/{id}/{urlCategoryName}/{country}/{language}, new { controller = Categories, action =
I have one route defined in application.ini like router.routes.xx.route = y/:var/:controller/:action/* router.routes.xx.defaults.controller = page
I have the following route: routes.MapRoute( Default, // Route name {controller}/{action}/{id}, // URL with
I have a route defined with several hardcoded (non-parameter) segments: routes.MapRoute(null, suggestion/list/by/{tag}, new {
heres my setup: http://www.example.com/<module>/<controller>/<action> I have this route defined: $router->addRoute( 'editUser', new Zend_Controller_Router_Route('admin/users/edit/:id', array(
I have got four routes defined like this: //Project list with page number routes.MapRoute(
I have the following route's defined: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute({resource}.axd/{*pathInfo}); routes.MapRoute(

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.