views:

33

answers:

1

Dear SO surfers,

I have a route defined as such:

    routes.MapRoute(
        "CustomerWithScreenName", // Route name
        "Customer/{sn}/{action}", // e.g. Customer/KingKong89/Schedule
        new { controller = "Customer", action = "Signup", id = "" } // Parameter defaults
    );

But when I formulate a link using ActionLink like this:

<%: Html.ActionLink("Click here", "Schedule", "Customer", new { sn = "KingKong89", new { @class="topLink" } )%>

The resulting hyperlink/URL comes out like this:

http://localhost:65071/Customer/Schedule?sn=KingKong89

It works but I expected ActionLink to use the known routes and construct the correct link which would be:

http://localhost:65071/Customer/KingKong89/Schedule

Is my expectation wrong? Is it the parametered URL how it should work?

Note: I don't specify {controller} in the route. I can't seem to mix this route with the more orthodox {controller}{action}{id} route - maybe I should stop trying to be clever and adopt the convention.

Thanks for your help. Luke.

UPDATE

I have a theory that without a proper route (including the {controller} special keyword), I won't be able to make ActionLink produce proper URLs.

So I'm now turning my sights on why this simple route doesn't work:

routes.MapRoute(
        "ControllerWithScreenName", // Route name
        "{controller}/{sn}/{action}", // e.g. Customer/Dave/Schedule
        new { controller = "Customer", action = "Signup", sn = "" } // Parameter defaults
        );

As the only route, along with the default "Root", it doesn't play ball. If the controller name is added specifically (as in the first snippet above) it does.

UPDATE 2

Due to time constraints, I'm giving up on my param-before-action style URL and going back to the default controller\action\param - its a small thing in the grand scheme of making progress and not feeling frustrated and depressed.

UPDATE 3

I'm going to uncomment all my smart-ass routes and try using a RouteLink and specify the type of route to use. I think this is the raison d'etre of the RouteLink comment.

UPDATE 4

This is no good. This requires the app has knowledge of the routes to take which means I lose the option to just alter the routing in future and have all the links update automatically.

+2  A: 

First thing, you are defining your route with an unused parameter, you should define your route like this

routes.MapRoute(
    "CustomerWithScreenName", // Route name
    "Customer/{sn}/{action}", // e.g. Customer/KingKong89/Schedule
    new { controller = "Customer", action = "Signup", sn= "" } // Parameter defaults
);

The sn in your definition is not defined in your parameters instead you are defining id. Define your Route before the Default Route.

Second, your ActionLink should look like this

<%= Html.ActionLink("Click here", "Schedule", "Customer", new { sn = "KingKong89" }, new { @class = "topLink" })%>

Lastly, in your Controller, your parameter for your Action Method Schedule should be sn not id

public ActionResult Schedule(string sn)
{
    return View();
}
rob waminal
Hi Rob, and thanks for your help. I've corrected the route (id>sn), the second is a typo on here, and the third is already how it's setup. It's still producing old URLs. I'm going to investigate further.
Luke Puplett
are you sure? I've tested this one before I post it, and it works fine on my end.
rob waminal
I just tested again to make sure and I get: <li><a class="topLink" href="/Customer/Schedule?sn=LukeSkywalker">LukeSkywalker</a></li>In the source. I'm giving up and using the standard. Sod it - haven't time. If I'd have written MVC 2 I'd have each action with its own route set via an attribute and no fuzzy matching logic. Cheers again for your assistance. Appreciated.
Luke Puplett
Cheers man, one last thing. Your default "Root" route should be at the buttom. I don't know about you but I'm sure it works on mine.. :)
rob waminal