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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:43:42+00:00 2026-06-03T23:43:42+00:00

I have a page route with an optional id parameter. Defined like so: routes.MapPageRoute(null,

  • 0

I have a page route with an optional “id” parameter. Defined like so:

        routes.MapPageRoute(null,
            "projects/{operation}/{id}",
            "~/Projects/ProjectWizard.aspx",
            true,
            new RouteValueDictionary(new
            {
                operation = "new",
                id = UrlParameter.Optional
            }),
            new RouteValueDictionary(new
            {
                operation = @"new|edit",
                id = @"\d*"
            })
        );

The route works fine, but when I get to the page if I haven’t included the optional “id” parameter then my code fails when trying to convert to integer.

        var operation = RouteData.Values["operation"].ToString();
        var projectId = RouteData.Values.ContainsKey("id") ? Convert.ToInt32(RouteData.Values["id"]) : 0;

This works great as long as I pass in an ID in the route like this:

http://webapp/projects/edit/123

projectId is equal to 123 just like it should be. But if I don’t include the ID:

http://webapp/projects/new

The route goes to the page fine but I get this error:

Unable to cast object of type ‘System.Web.Mvc.UrlParameter’ to type ‘System.IConvertible’.


It should be unable to find the key in the route data values and then assign a zero to projectId, but it doesn’t. I keep trying different ways to check for null for the “id” route value but it doesn’t seem to like that very much.

  • 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-03T23:43:43+00:00Added an answer on June 3, 2026 at 11:43 pm

    That would work perfectly were you dealing with MVC controller actions here and MapRoute.

    What I would do in your case is simply set the default for your id in the defaults instead of UrlParameter.Optional, so:

    
    
    routes.MapPageRoute(null,
        "projects/{operation}/{id}",
        "~/Projects/ProjectWizard.aspx",
        true,
        new RouteValueDictionary(new
        {
            operation = "new",
            id = "0"
        }),
        new RouteValueDictionary(new
        {
            operation = @"new|edit",
            id = @"\d*"
        })
    );
    
    

    then simply read your id like so:

    
    var operation = RouteData.Values["operation"].ToString();
    var projectId = int.Parse(RouteData.Values["id"].ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got four routes defined like this: //Project list with page number routes.MapRoute(
I have my views render to route requests like /controller/action (for instance, /Page/Create), so
I have a route like following, ideally I would like it to match: domain.com/layout/1-slug-is-the-name-of-the-page
HI I have the following routing table routes.MapRoute(null, Save, // Route name new {
I have there lines in my RegisterToutes : routes.MapRoute(Pages3, {url1}/{url2}/{url3}, MVC.Page.RedirectTo(), new { url1
I have the following default and only route: routes.MapRoute( Default, // Route name {controller}/{action}/{id},
I have the following URL in mind: /restaurants/italian/miami.html /restaurants/italian/miami-p2.html Using these routes routes.MapRoute(null, {category}/{branch}/{city}-p{page}.html,
I have two MapRoute in global asax.` routes.MapRoute( AutoGeneratedURLHandler, // Route name {modulepath}/{controller}/{action}/{id}, //
So right now I have two routes: routes.MapRoute( Default, // Route name {controller}/{action}/{id}, //
Most of the routes I use are based on the default route defined in

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.