Jon
Jon

Reputation: 40032

ASP.Net MVC Routing Issue..Again

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?

Upvotes: 1

Views: 177

Answers (2)

Martin
Martin

Reputation: 11041

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.

Upvotes: 1

Razzie
Razzie

Reputation: 31222

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).

Upvotes: 0

Related Questions