问题描述
我使用了自定义的mvc路由处理程序.但是,当我在视图中使用Html.BeginForm或Ajax.BeginForm时,Action参数会意外更改.我的密码在上方
I used custom mvc route handler. But When I use Html.BeginForm or Ajax.BeginForm in the view, Action parameters change unexpectedly. My codes are above
我的注册路由方法是:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("alias",
"{alias}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
).RouteHandler = new FriendlyUrlRoutehandler();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
}
我的MvcRouteHandler是:
My MvcRouteHandler is :
public class FriendlyUrlRoutehandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var friendlyUrl = (string)requestContext.RouteData.Values["alias"];
if (friendlyUrl != null)
{
if (friendlyUrl.Equals("man-parfume"))
{
requestContext.RouteData.Values["controller"] = "ManParfume";
requestContext.RouteData.Values["action"] = "Index";
}
else if (friendlyUrl.Equals("ring"))
{
requestContext.RouteData.Values["controller"] = "Ring";
requestContext.RouteData.Values["action"] = "Index";
}
else if (friendlyUrl.Equals("glases"))
{
requestContext.RouteData.Values["controller"] = "Categories";
requestContext.RouteData.Values["action"] = "Index";
requestContext.RouteData.Values["id"] = 10;
}
}
return base.GetHttpHandler(requestContext);
}
}
我的观点是:
@using (Ajax.BeginForm("Index", "Home", new AjaxOptions { HttpMethod = "Post" }))
@using (Html.BeginForm("Index", "Home"))
浏览器中的最终错误动作如下:
Finally wrong action in browser looks like this :
我以begin形式将控制器写为home,但是在呈现Html.BeginForm时它发生了变化.
I wrote controller as a home in begin form but it changed when Html.BeginForm rendered.
如何将表单发布到首页/索引?
How can I post the form to Home/Index?
推荐答案
您可以使用<form action="@Url.RouteUrl("Default", new { controller="Home", action="Index"}" method="post">
代替Html.BeginForm
.
关于Ajax.BeginForm
,它可以在jQuery函数上更改:
As to Ajax.BeginForm
it may be changed on jQuery function:
$.ajax({
type: "POST",
url: "@Url.RouteUrl("Default", new { controller="Home", action="Index"}",
data: data,
success: success,
dataType: dataType
});
这篇关于Html:BeginForm中的MVC.NET自定义根处理程序错误操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!