views:

40

answers:

2

Hi there

I am trying to build a route with the format of {controller}{action}{classificationID}{reviewID}{CategoryID}\

but I want to be able to address it without the final CategoryID and have that default to 1 where it isn't given.

Here are my routes:

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "SchoolRouteNoCategory", // Route name
            "{controller}/{action}/{ClassificationID}/{ReviewID}/", // URL with parameters
            new { controller = "School", action = "Edit", ClassificationID = "SBP", ReviewID = "SAR" } // Parameter defaults
        );

        routes.MapRoute(
            "SchoolRoute", // Route name
            "{controller}/{action}/{SchoolID}/{ReviewID}/{CategoryID}/", // URL with parameters
            new RouteValueDictionary { { "action", "index" }, { "SchoolID", UrlParameter.Optional }, { "ReviewID", UrlParameter.Optional }, { "CategoryID", UrlParameter.Optional } } // Parameter defaults
        );

and here are my controller signatures for the edit action:

    public ActionResult Edit(string SchoolID, string ReviewID) {}

    public ActionResult Edit(string SchoolID, string ReviewID, int CategoryID) {}

The first Edit Action will just add the default CategoryID and forward it to the second Edit Action.

My problem is that when i try to build the ActionLink in my Index view, I get the following URL:

http://localhost:21271/School/Edit?ClassificationID=SBP&ReviewID=SAR

where as I should have

http://localhost:21271/School/Edit/SBP/SAR/

The code for the ActionLink is

<%: Html.ActionLink("Edit", "Edit", new { item.ClassificationID, item.ReviewID}) %>

I'm starting to get the feeling that I know even less about routing than I thought, but most of the tutorials I can find only focus on very simply routes and don't cover anything complicated. Are there any tutorials that cover the sort of routing I am trying to do?

EDIT:

Got a new routing issue, thought I'd try posting it here first rather than creating a new thread.

I have the following ActionLink on my edit page:

<%: Html.ActionLink(Model.Categories[i].name, "Edit", new { AcadPeriod = Model.AcadPeriod, ClassificationID=Model.ClassificationID, ReviewID=Model.ReviewID, CategoryID=Model.Categories[i].category_id })%>

which results in the following url:

http://localhost:21271/School/Edit?ClassificationID=SBP&amp;ReviewID=SAR&amp;CategoryID=2

If I run the route debugger, I get the following output.

Matched Route: {controller}/{action}/{AcadPeriod}/{id}
Generated URL: /School/Edit/1011/SBP/SAR/2 using the route "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID}"

However, in the routes table, it says that {controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID} doesn't match?

It seems to be telling me two different things on the same page.

This is my routes in global.asax as they currently stand:

        routes.MapRoute(
            "SchoolRoute1",
            "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}/{CategoryID}",
            new { controller = "School", action = "Edit", AcadPeriod = "1011", CategoryID = "1" }
        );

        routes.MapRoute(
            "SchoolRouteNoCategory",
            "{controller}/{action}/{AcadPeriod}/{ClassificationID}/{ReviewID}",
            new { controller = "School", action = "Edit", AcadPeriod = "1011", CategoryID = "1" }
        );

        routes.MapRoute(
            "SchoolRoute",
            "{controller}/{action}/{ClassificationID}/{ReviewID}/{CategoryID}",
            new { controller = "School", action = "Edit", CategoryID = "1" }
        );


        routes.MapRoute(
            "SchoolIndex", // Route name
            "{controller}/{action}/{AcadPeriod}/{id}", // URL with parameters
            new { controller = "School", action = "Index", AcadPeriod = "1011", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "SchoolIndex1", // Route name
            "{controller}/{action}/{AcadPeriod}", // URL with parameters
            new { controller = "School", action = "Index", AcadPeriod = "1011" } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

Any ideas?

+2  A: 

You need to put your custom routes above the default action/controller/id route. List them in the opposite order of what u have.

lcrepas
+3  A: 

Firstly, routes are evaluated in order from top to bottom. The first route that you have listed will match anything so your additional routes will never even be considered.

This means that the most specific routes should go at the top. And the default, "catch-all" route should go at the bottom.

I think the route you want is (are the second & third route supposed to be related?):

routes.MapRoute(
    "SchoolRoute",
    "{controller}/{action}/{SchoolID}/{ReviewID}/{CategoryID}",
    new { controller = "School", action = "Edit", CategoryID = "1" }
);

If it's not optional and there's no default value, there's no need to list it in the third line of the route declaration.

Jon
I tried it in the opposite order before and I ended up with an actionlink with no parameters at all. Not sure why that is but the route you provided above has solved my problem perfectly. Thank you very much. Think I need to read up on routing...:)
hermiod