3

Using MVC, I have an html form helper in my view:

using (Html.BeginForm("ActionOne", "ControllerOne")) ...

Using the default route, the output for the action attribute is as expected:

<form action="/ControllerOne/ActionOne" ...

But registrering a new route with seemingly no matches affects the output.

Routing code:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.Add("testRoute", new Route("MyUrl", new MvcRouteHandler()));

    routes.MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index"});
}

Output:

<form action="/MyUrl?action=ActionOne&amp;controller=ControllerOne"

Is this by design or am I mising something fundamental?

Cheers!

3 Answers 3

10

I have experienced this exact problem. I'm not sure exactly why the System.Web.Mvc.HtmlHelper seems to just use the first non-ignore route in the routetable to generate links etc from but I have found a workaround for the "BeginForm" issue.

If you have named your "Default" route in the Global.asax.cs, for example:

routes.MapRoute("Default", "{controller}/{action}", new {controller = "Home", action = "Index" });

Then you can use the Html.BeginFormRoute method and call the name of the "Default" MVC route, then name the controller and action specifically, resulting in the correct url:

using (Html.BeginRouteForm("Default", new { controller="YourController", action = "YourFormAction" })) { }

HTH

2
  • Hey, that works. Its still notm ideal thouh, as changing routing on a global scale would need you to edit every occurrence of this :(
    – theGecko
    Commented Nov 18, 2009 at 10:02
  • I agree it isn't ideal and may require some work to update all the uses of BeginForm -> BeginRouteForm if being done on an existing webapp, but if you have all your forms set up to use the default route (as specified above) then as long as you keep that route intact any other changes to routing will not affect the forms, so it is a robust solution.
    – Stelloy
    Commented Nov 18, 2009 at 10:18
1

Try this

public static void RegisterRoutes(RouteCollection routes)
{
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.Add("testRoute", new Route("MyUrl/***{action}/{controller}***", new MvcRouteHandler()));

        routes.MapRoute("Default", "{controller}/{action}", new { controller = "Home", action = "Index"});
}

I think it should solve your problem.

2
  • This outputs: <form action="/MyUrl/***ActionOne/ControllerOne***"
    – theGecko
    Commented Nov 18, 2009 at 9:58
  • sorry, the *** part was for bold and italic, but it somehow didn't show. What I meant was MyUrl/{action}/{controller}
    – Wei Ma
    Commented Nov 18, 2009 at 10:49
-1

Add this before default route

      routes.MapRoute("", "ControllerOne/ActionOne", new { controller = "ControllerOne", action = "ActionOneOne"});
1
  • Would we have to register every possible controller/action pair?
    – theGecko
    Commented Nov 18, 2009 at 9:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.