服务路由的 ASP.NET MVC 表单操作

发布于 2024-12-12 09:56:38 字数 883 浏览 0 评论 0原文

我正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2024-12-19 09:56:38

尝试使用如下,

//New rule
RouteTable.Routes.Add(
    new ServiceRoute(
    "UserWebservice", 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
  );

我认为将路由添加的代码更改为 参考链接应该有效。
另请查看此博客以创建 动态服务路线

try using as below,

//New rule
RouteTable.Routes.Add(
    new ServiceRoute(
    "UserWebservice", 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
  );

I think changing the code for route add as Reference link should work.
Also check this blog for creating Dynamic service routes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文