0

I'm trying to change the URL displayed in the user's browser from Happy/Balloons to happy-times/balloon-pops. There are many links in the project to the action "Balloons", so rather than change those links I'd like to change the global.asax so that a different URL appears for the same action. The original MVC Route looks like:

routes.MapRoute(
            "Happy.Balloons",
            "Happy/Balloons/{groupId}/{paymentType}/{mortgageValue}/{province}",
            new { controller = "Happy", action = "Balloons" },
            new { groupId = "\\d+", paymentType = "\\d+", mortgageValue = "\\d+", province = "\\d+" }
        );

I've changed the code to

routes.MapRoute(
            "Happy.Balloons",
            "happy-times/balloon-pops/{groupId}/{paymentType}/{mortgageValue}/{province}",
            new { controller = "Happy", action = "Balloons" },
            new { groupId = "\\d+", paymentType = "\\d+", mortgageValue = "\\d+", province = "\\d+" }
        );

I thought this second parameter was the URL displayed, but I'm getting a: "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." error. Is there a simple way to do this by modifying parameters in MapRoute? If so, how?

Question Follow-up: Change URL of action in mvc

Community
  • 1
  • 1
valen
  • 807
  • 1
  • 16
  • 43

2 Answers2

2

You're doing it wrong.

Changing the route mapping will not change the name of the controller. Change the controller and action name, and map your route as {controller}/{action} like it is by default. Then you can set your default controller and action as you have before.

Then your controller will have to be renamed to happy-times and your action renamed to balloon-pops

I should note that it will look more like this:

{controller}/{action}/{groupId}/{paymentType}/{mortgageValue}/{province}

Here's a good resource on the topic.

http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

as brought to you by this SO post https://stackoverflow.com/a/2375293/1178921

**Looks like you might be trying to see more ways to do this.

This post talks about attributes you can use to route URLS differently. ASP.NET MVC Routing Via Method Attributes

Additionally, your way of changing the url in the mapping CAN work, but isn't what I thought your intent was. Zach dev had this one dead on in that case. Your optional parameters need to be marked as such with UrlParameter.Optional

Community
  • 1
  • 1
deltree
  • 3,756
  • 1
  • 30
  • 51
  • So there is no way to rewrite a controller name when rendering a specific URL? That is, the URL will always have the same name as a controller? Thanks for the resources btw, will definitely give them a look and post any solution I come up with – valen May 29 '12 at 16:10
2

this works, (replacing urlParameter.Optional to default values)

routes.MapRoute( _
            "Happy.Balloons", _
            "happy-times/ballons-pops/{groupId}/{paymentType}/{mortgageValue}/{province}", _
            New With {.controller = "happy", .action = "Balloons", .groupId = UrlParameter.Optional, .paymentType = UrlParameter.Optional, .mortgageValue = UrlParameter.Optional, .province = UrlParameter.Optional} _
    )

but... it's a bad practice (really bad one)

you can learn about route maps here

Zach dev
  • 1,610
  • 8
  • 15