ASP.Net MVC Routing issue with Html.BeginForm
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(RouteC开发者_高级运维ollection 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&controller=ControllerOne"
Is this by design or am I mising something fundamental?
Cheers!
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
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.
Add this before default route
routes.MapRoute("", "ControllerOne/ActionOne", new { controller = "ControllerOne", action = "ActionOneOne"});
精彩评论