tags:

views:

94

answers:

2

I don't know whether I'm just stupid (don't answer that!) or I'm fighting MVC routing but I have another problem.

In my Controller I do a RedirectToRoute("ErrorRoute") which renders a 404 View which has a MasterPage.

In the MasterPage I have a top navigation which has links such as /homepage and /news but when I am in the 404 View the navigation is /error/homepage and /error/news.

I have changed my route in Global.asax from this

routes.MapRoute(
             "ErrorRoute",                      // Route name
             "Error/Error404",                // URL with parameters
             new { controller = "Error", action = "Error404" }

             );

to this

routes.MapRoute(
             "ErrorRoute",                       // Route name
             "Error/Error404",                  // URL with parameters
             new { controller = "Error", action = "Error404" }
             , new { action = "Error404" }
             );

to see if that would help but I get a "No route in the route table.." error when I call RedirectToRoute

Can you please help?

+1  A: 

Are your Links ActionLinks?

Use this:

Html.RouteLink("Link Title", new { controller="Home" action="Action" });

EDIT

Oh, Add a new Route:

routes.MapRoute(
             "homePage",                      // Route name
             "homePage/",                // URL with parameters
             new { controller = "Home", action = "HomePage" }
             );

And repeat for news

ANOTHER EDIT

After reading the comments again, if your menu is on every page, what you should be is create a base controller:

public class MyBaseController : Controller
{
    return ViewData["menu"] = List<MenuClass>;
}

Then all your Controllers (home, error) inherit this:

public class HomeController : MyBaseController

Then in your master page, loop through the ViewData["menu"]:

<% foreach (MenuClass in ViewData["menu"]) { %>
<li>
    <%=Html.RouteLink(MenuClass.LinkTitle, new { controller = "Home", action = MenuClass.Action }) %>
</li>
<% } %>

Note: this is all from scratch, so there may be errors, but this is what I did on my last MVC project.

Martin
My Menu is an HTMLExtension that I wrote to get menu items out of the database, loops through them and then just returns an HTML Unordered list to the page. How would I modify it to do ActionLink/RouteLinks and then output the unordered list?
Jon
Its ok I have amended my Menu extension so instead of <li><a href="/news">News</a></li> I have got <li> + helper.RouteLink(urlTitle, new { controller = "WebPage", action = "Details", pageName = linkUrl }) +</li>. What is the difference between this approach and using ActionLink?
Jon
ActionLink directs you to an action in the same controller (unless you specify the controller in the parameters). RouteLink makes you choose the route (controller, action, id).
Martin
A: 

How are those links in the MasterPage defined? If they link to 'news', you should use '/news' and '/homepage', or else it will append it to current url (in your case, /error).

Razzie