3

I have a controller called Diary with an action called View.

If I receive a URL in the form "Diary/2012/6" I want it to call the View action with year = 2012 and month = 6.

If I receive a URL in the form "Diary" I want it to call the View action with year = [current year] and month = [current month number].

How would I configure the routing?

2 Answers 2

2

In your routes, you can use the following:

routes.MapRoute(
                "Dairy", // Route name
                "Dairy/{year}/{month}", // URL with parameters
                new { controller = "Dairy", action = "Index", year = DateTime.Now.Year, month = DateTime.Now.Month });

If year/month are not provided, the current values will be sent. If they are provided, then those values will be used by the route.

  • /Dairy/ -> year = 2012, month = 6
  • /Dairy/1976/04 -> year = 1976, month = 4

EDIT

In addition to the comment below, this is the code used to create a new project using the above criteria.

Global.Asax

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "Dairy/{year}/{month}", // URL with parameters
        new { controller = "Dairy", action = "Index", year = DateTime.Now.Year, month = DateTime.Now.Month } // Parameter defaults
    );

}

DairyController

public ActionResult Index(int year, int month)
{
    ViewBag.Year = year;
    ViewBag.Month = month;
    return View();
}

The View

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

Month - @ViewBag.Month <br/>
Year - @ViewBag.Year

Results:

  • /Dairy/1976/05 -> outputs 1976 for year and 5 for the month
  • / -> outputs 2012 for year and 6 for month
Sign up to request clarification or add additional context in comments.

6 Comments

If I don't supply the parameters in the URL, I get the old 'parameters dictionary contains null entry' schtick.
@David - Do you have other routes that might be mucking with this one? I just created a new project using the above and had no issues with the parameters dictionary.
Oh, interesting. I'll delete my other routes and check.
@David - You might need to explicitly call out the dairy controller in your route so that the routes match up correctly with any other routes that you may have defined. Updated examples above.
I just had to rearrange my routes! Thank you very much for your help.
|
2
routes.MapRoute(
    "DiaryRoute",
    "Diary/{year}/{month}",
    new { controller = "Diary", action = "View", year = UrlParameter.Optional, month = UrlParameter.Optional }
);

and the controller action:

public ActionResult View(int? year, int? month)
{
    ...
}

3 Comments

If I don't supply the parameters in the URL, I get the old 'parameters dictionary contains null entry' schtick.
Did you notice how I have declared the parameters as nullable integers in the action signature => int? instead of int? Did you do the same thing?
Sorry, my bad, you're right. I'm marking Tommy's answer because the setting of default values is handled in the routing, which I prefer. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.