服务路由的 ASP.NET MVC 表单操作
我正在开发 ASP.NET MVC 应用程序。我一直只使用默认路由规则。我有许多使用如下代码呈现表单的视图:
@using (Html.BeginForm("ForgotPassword", "Register", FormMethod.Post))
这一直工作正常。表单操作将发布到 /myapp/register/forgotpassword
并且一切正常。
现在需要将一些服务端点添加到同一应用程序中。所以我在默认路由之上添加了一些新路由。路由设置现在看起来像:
//New rule
RouteTable.Routes.Add(
new ServiceRoute(
"api/user", new MyCustomerServiceHostFactory(),
typeof(UserWebservice)));
//Default rule
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
添加新规则后,我的所有表单都损坏了。检查 HTML,我可以看到表单操作是 /myapp/api/user?action=ForgotPassword&controller=Register'
,这是完全错误的。
所以我的问题是:如何在不破坏所有现有表单的情况下路由新服务?
还有奖励积分:这里到底发生了什么?
I'm working on an ASP.NET MVC app. I have been using just the default routing rule. I have a number of views that render forms using code like this:
@using (Html.BeginForm("ForgotPassword", "Register", FormMethod.Post))
This has been working fine. The form action would post to /myapp/register/forgotpassword
and everything works great.
Now need to add some service endpoints into the same application. So I added some new routes above the default one. The routing setup now looks like:
//New rule
RouteTable.Routes.Add(
new ServiceRoute(
"api/user", new MyCustomerServiceHostFactory(),
typeof(UserWebservice)));
//Default rule
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
After I added the new rule all my forms broke. Inspecting the HTML, I can see that the form action is /myapp/api/user?action=ForgotPassword&controller=Register'
, which is totally wrong.
So my question is: how do I route the new service without breaking all my existing forms?
And for bonus points: what the heck is going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用如下,
我认为将路由添加的代码更改为 参考链接应该有效。
另请查看此博客以创建 动态服务路线。
try using as below,
I think changing the code for route add as Reference link should work.
Also check this blog for creating Dynamic service routes.