mvc html.begiform action値が正しくない問題

84812 ワード

from: 
http://stackoverflow.com/questions/1754771/asp-net-mvc-routing-issue-with-html-beginform
 
Using MVC,I have an.form helper in my view:
using (Html.BeginForm("ActionOne", "ControllerOne")) ...
Using the default route、the output for the action atribute is as expected:
<form action="/ControllerOne/ActionOne" ...
But registering a new route with seemenglyのmatches affects the out put.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 fundenmental?
 
I have experienced this exact proble.I'm not sure exactly why the System.Web.Mvc.HtmlHelper seems to just the first non-ignore route in the route to genese links etc the from foun
If you have named your「Default」route in the Global.asax.cs,for example:
routes.MapRoute("Default", "{controller}/{action}", new {controller = "Home", action = "Index" });
 
The n you can use the Html.BeginnFormRoute method and cal the name of the「Default」MVC route,then name the control and action specificaly,reulting in the corecturl:
using (Html.BeginRouteForm("Default", new { controller="YourController", action = "YourFormAction" })) { }
 
 
 
Hey、that works.Its still notm ideathouh、as chaging routing on a global scale would need you to edit everoccurrence of this: – 
theGecko 
Nov 18'09 at 10:02
I agree it isn't ideal and may require some work to udate all the uss of Begin Form->Begin RouteForm if being done on on existing webapp,but if you have all your forms set up to use the deult fault routethen as long as you keep that route intact any other changes to routing will not affect the forms,so it is a robust soution. – 
Step hen Lloyd 
Nov 18'09 at 10:18