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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:28:32+00:00 2026-06-08T23:28:32+00:00

So, I’ve been trying for a few hours now to workaround something that should

  • 0

So, I’ve been trying for a few hours now to workaround something that should theoretically be very simple. Let’s take this sample url:

http://sample.com/products/in/texas/dallas

This maps onto a specific route:

routes.MapRoute(
    "products",
    "products/in/{state}/{city}",
    new { controller = "Products", action = "List", state = UrlParameter.Optional, city = UrlParameter.Optional });

In my action method, I can do lookups to make sure “texas” and “dallas” exist, and that “dallas” exists within Texas. That’s all fine and dandy. However, in the situation where the city doesnt exist (either because the geo is incorrect, or mispelled), I want it to back up the state level. Example:

http://sample.com/products/in/texas/dallax

That should issue a redirect to

http://sample.com/products/in/texas

The “easy” way to do this was to simply issue a Redirect call like so:

return Redirect("/products/in/" + stateName);

However, I’m trying to decouple this from the URL structure; for example, if we ever decided to change how the route looks (say, change the pattern to products/around/{state}/{city}), then I would have to know that I need to make updates to this controller to fix the URL redirect.

If I can make a solution that just inspects the route values and can figure things out, then I don’t have to worry if I change the route pattern, because the route values could still be figured out.

Ideally, I would have liked to do something like this:

return RedirectToRoute(new { controller = "Products", action = "List", state = state });

(Note, that is is a simplified example; the “required” route pieces like controller name and action method name would be determined by Generic argument and Expression inspection respectively).

That actually performs the redirect, HOWEVER, the route values from the current request get appended onto the redirect and thus you get in a redirect loop (note that I didn’t include the city route value in the route object). How do I stop the “city” route value from being included in this redirect?

I’ve tried the following things to get rid of the route value:

  1. Compose my own RouteValueDictionary / anonymous route data object and pass that to the overload of RedirectToRoute.
  2. Create my own action result to inspect RouteTable.Routes and find the route myself, and do the replacement of the tokens myself. This seems the most “kludgy”, and would seem to be re-inventing the wheel.
  3. Make a method like RedirectWithout that takes a key value and calls RedirectToRouteResult.RouteValues.Remove(key) – which also didnt work.
  4. I’ve also attempted to add a null value for the key I don’t want to add; however, this alters the route to something that isnt correct – ie new { controller = "Products", action = "List", state = stateName, city = (string)null } issues a redirect to /Products/List?state=Texas which is not the right URL.

It all seems to stem from the RedirectToRoute taking the current request context to construct the virtual path data. Is there a workaround?

  • 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-08T23:28:33+00:00Added an answer on June 8, 2026 at 11:28 pm

    If you were using T4MVC, you should be able to do something like this:

    return RedirectToAction(MVC.Products.List(state, null));
    

    Have you tried this?

    return RedirectToRoute(new 
    {
        controller = "Products", 
        action = "List", 
        state = state,
        city = null, 
    });
    

    Reply to comments

    Maybe MVC is confused because your optional parameter is not at the end. The below should work with the above RedirectToRoute that specifies city = null:

    routes.MapRoute(
        "products",
        "products/in/{state}/{city}",
        new
        {
            controller = "Products",
            action = "List",
            // state = UrlParameter.Optional, // only let the last parameter be optional
            city = UrlParameter.Optional 
        });
    

    You can then add another route to handle URL’s where state is optional:

    routes.MapRoute(null, // I never name my routes
        "products/in/{state}",
        new
        {
            controller = "Products",
            action = "List",
            state = UrlParameter.Optional 
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.