1

Is there a way to change the URL of a given action in mvc without changing the action or controller called?

If so, how would this be done on the following MapRoute:

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

The desired URL is: ".../estate-support/deal-closing/...". Currently it displays as ".../Estate/CloseDeal/..."

The button linking to this action looks like:

 <button detail="@Url.Action("CloseDeal", new { groupId = info.GroupId })" class="orange">

EDIT 1:

Tried changing to:

routes.MapRoute(
        "Estate.CloseDeal",
        "estate-support/deal-closing/{groupId}/{paymentType}/{mortgageValue}/{province}",
        new { controller = "Estate", action = "CloseDeal" },
        new { groupId = "\\d+", paymentType = "\\d+", mortgageValue = "\\d+", province = "\\d+" }
    );

This returned error: The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Edit 2:

Changing the second string worked for all routes but this one - the difference being, this route has additional parameters (groupID, paymentType etc.).

tereško
  • 58,060
  • 25
  • 98
  • 150
valen
  • 807
  • 1
  • 16
  • 43
  • @valen....you have "estate-support/deadl-closing"...where deal is misspelled as deadl. Is that a type-o in this question or in your code? – MikeTWebb May 29 '12 at 17:23
  • Oops, that's a typo in this question. But shouldn't it not matter...since that's just a string to rewrite the URL to? – valen May 29 '12 at 17:24
  • Gotcha......but, if it were a type-o in the code then the route would fails because it wouldn't be able to find that View. It would be like www.googdle.com producing an error – MikeTWebb May 29 '12 at 17:27

3 Answers3

3

Just replace "Estate/CloseDeal" in the second string with "estate-support/deal-closing" - should work fine.

In this particular case it's this easy because the route is not parameterised over the controller and action names - i.e. the route doesn't have "{controller}/{action}" in it.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
  • Doing the above results in the error: The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. – valen May 29 '12 at 16:47
  • Then presumably whatever it is that links to that url is not using routing to generate it - my guess you have a page/action somewhere that's hard-coded a `Estate/CloseDeal` in there somewhere; it should probably instead be using `Url.RouteUrl`/`Url.Action` or, when generating a link on a web page, `Html.RouteLink`/`Html.ActionLink` – Andras Zoltan May 29 '12 at 16:54
  • Please see edit...I'm in fact calling "Url.Action", and it's not working – valen May 29 '12 at 17:18
  • I'm assuming that the Action call there is being called in a view that's rendered under the Estate controller? If not, you have to put the controller in as well. Also, what's the url when you get the 404? – Andras Zoltan May 29 '12 at 18:14
  • Thanks for sticking with this. That's correct, the view CloseDeal is rendered under the Estate controller. The URL is: ".../Estate/CloseDeal/..." when code under Edit 1 is executed. – valen May 29 '12 at 18:26
  • It could be because of the constraints placed on the route - all of these: `{paymentType}/{mortgageValue}/{province}` have constraints that they must all be numbers - but in your `Url.Action` call you're not passing them. So I wonder if the url that is generated is matching a different route. Try providing those other values in your route values object in `Url.Action`. – Andras Zoltan May 30 '12 at 07:23
  • Hi Andras. So you're right, the problem is with the parameters. I tried setting up url changes for other routes, and they all worked fine. This is the only route with parameters, and I've determined that it's in fact the route being called. The Url.Action call IS passing the parameters though. It's just that the URL displayed in the browser when this action is called shows no change regardless of what I put in the second string – valen Jun 01 '12 at 16:52
1

You can also apply a route directly to an action (as shown below) or controller class itself using RouteAttribute

// Your controller class
// (You could add a Route attribute here)]
public class Estate
{
    // Directly apply a route (or 2?!) to this action
    [Route("estate-support/deal-closing")]
    [Route("clinched")] // You can add multiple routes if required
    public ActionResult CloseDeal()
    {
        ...
    }

    // And of course you can parameters to the route too, such as:
    [Route("customers/{customerId}/orders/{orderId}")]
    public ActionResult GetOrderByCustomer(int customerId, int orderId)
    {
        ...
    }

    ...
}

For this to work you need to enable it by calling MapMvcAttributeRoutes in RouteConfig.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    // Enable mapping by attibute in the controller classes
    routes.MapMvcAttributeRoutes();
    ...
}

More info here from learn.microsoft.com: Attribute Routing in ASP.NET
and here: C# Corner - route attribute in MVC

noelicus
  • 14,468
  • 3
  • 92
  • 111
0

You need either to update parameters of Url.Action calls or make you route that you want to be rendered to match the parameter of Url.Action call (i.e. name should match if using Url>Action override that uses name).

Note that you may want to map old url to new one with adding route that will simply redirect to new one if you expect people could added old Urls to favorites.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • so button would call @Url.Action("deal-closing", new { groupId = info.GroupId }), given EDIT 1's code?. I'm a little confused, since the Action still reads "CloseDeal" and the Button is calling "CloseDeal". – valen May 29 '12 at 17:27
  • Not sure - Url.Action needs to find matching routing rule based on arguments. Check out this http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx for some info. – Alexei Levenkov May 29 '12 at 17:45