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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:02:16+00:00 2026-06-04T02:02:16+00:00

I am using ASP.NET MVC3 with Razor and would like to define a route

  • 0

I am using ASP.NET MVC3 with Razor and would like to define a route that would allow me to receive URLs such as this. Note I am new at MVC3:

/State/Florida/Town/Fort_Lauderdale
/State/Texas/Town/Irvine
/State/Alabama/Town/Auburn

I would like that to be handled by the existing “States” controller (StatesController class) using the “Town” action. I have something like this:

routes.MapRoute(
   "Towns",
   "{controller}/{state}/{action}/{town}",
   new { controller = "States", action = "Town", state= UrlParameter.Optional, town = UrlParameter.Optional }

Obviously neither the state nor the town parameters are optional, how do I go about that? I don’t think the above route is correct, isn’t there a way to indicate that the parameter is NOT optional?.

Another situation here is that while with the above I pretended to handle the towns with a single action in a controller Town(string town), I would still need to be able to parse individual town files with Razor. I originally thought of having the Town() controller method to use the town parameter to serve a town’s HTML file but as it turns out, I also want to be able to have dynamic data on a town description. Does that means that my best solution is to create a dedicated Town controller and create one action method for each of the MANY towns?

Any suggestions to better handle that?

  • 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-04T02:02:17+00:00Added an answer on June 4, 2026 at 2:02 am

    Just remove the UrlParameter.Optional

    routes.MapRoute(
        "Towns",
        "{controller}/{state}/{action}/{town}",
        new { controller = "States", action = "Town" }
    

    I would just go for something more explicit like:

    routes.MapRoute(
        "Towns",
        "State/{state}/Town/{town}",
        new { controller = "States", action = "Town" }
    

    Indeed, there’s a typo in the previous one (based on the one you posted) because the controller is States by default and State by the url.

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

Sidebar

Related Questions

I have something like this in Site.Master in an asp.net mvc3 (not razor) project:
I have created a link for deletion in asp.net mvc3 razor view like this:
I'm using jQuery Validate Unobtrusive with ASP.NET MVC3 Razor. I have a page with
I am using ASP.Net-MVC3 and I use this query in Layout.cshtml : $(function() {
I am building an applicaiton using asp.net mvc3. I added two layers to this
I had created a webpage using asp.net mvc3 razor. I have two master pages
I am creating a new web application using ASP.NET MVC3 Razor and HTML 5.
Here's the issue at hand: I have developed an ASP.NET MVC3 application using Razor.
I'm currently developing a site using ASP.Net MVC3 with Razor. Inside the View/Shared folder,
I have an asp.net mvc3 Razor app being developed in VS2010 using Git for

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.