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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:16:40+00:00 2026-05-13T13:16:40+00:00

I have a controller named Movie , with an action named ByYear , which

  • 0

I have a controller named Movie, with an action named ByYear, which takes the year as a parameter :

    public ActionResult ByYear(int year)
    {
        ViewData["Title"] = string.Format("Movies released in {0}", year);
        var repository = MvcApplication.GetRepository();
        var movies = repository.Medias
                                .OfType<Movie>()
                                .Where(m => m.Year == year);
        return View("Index", movies);
    }

I’d like to access this action with the following URL : /Movie/ByYear/{year}, but the only valid route for this action is this : /Movie/ByYear?year={year}.

I tried to add new routes in my application’s RegisterRoutes method, but I can’t find a way to get the desired result…

Could anyone tell me how to achieve that ?

Note: this is actually very similar to this question, but no answer was accepted, and the highest voted answer makes no sense to me as I’m completely new to MVC…

  • 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-13T13:16:40+00:00Added an answer on May 13, 2026 at 1:16 pm

    Change the name of your parameter year to id and this will match the default route that MVC adds to your project.

    So for further clarification, let’s take a look at the default route added by ASP.NET MVC:

    routes.MapRoute(
        "default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = "" }
    );
    

    In this route you can see three tokens that are named specifically for controller, action, and the third token which is passed to the action is id. When a request comes into your application, ASP.NET MVC will analyze the routes that are currently mapped and try to find a method signature that matches them by using reflection against your controllers.

    When it looks at your Movie controller, it sees an action called ByYear, however that method takes an integer called year, not id. This is why you end up with something like /Movie/ByYear?year={year} when you create an ActionLink for that particular Action. So to fix this, you have two options:

    The first and most simple method to fix this is to just change the method signature for your Action to accept a parameter named id which is what I recommended above. This will work fine, but I can see where it might cause a little bit of confusion when you go back to that source later and wonder why you called that parameter id.

    The second method is to add another route that matches that method signature. To do this, you should open your Global.asax and just add the following (untested, but should work):

    routes.MapRoute(
        "MoviesByYear",
        "Movies/ByYear/{year}",
        new { controller = "Movie", action = "ByYear" }
    );
    

    This route is hard-coded, yes, but it won’t break the other routes in your system, and it will allow you to call the method parameter year.

    EDIT 2: Another thing to note is that the routing engine will stop on the first route it finds that matches your request, so any custom routes like this should be added before the default route so you are sure they will be found.

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

Sidebar

Related Questions

I have a controller named Content, which has an Index action, which is associated
i have a controller named PagesController which used to have an action named contest
I have the following controller: public ActionResult Search(string Name, int? Friend, int? Page) It
I have a Controller named Author . Inside the controller, I have defined a
I have a controller/model hypothetically named Pets. Pets has the following declarations: belongs_to :owner
I have three views each represented by a View controller named firstVC, secondVC and
I have a action on my controller (controller name is 'makemagic') called 'dosomething' that
I have a get-parameter with name controller . When I try to pass it
I have the following route: routes.MapRoute( Default, // Route name {controller}/{action}/{id}, // URL with
how can I jump to another action in controller? I have form and several

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.