使用基于 ASP.NET MVC 属性的路由映射器

发布于 2024-09-06 07:04:28 字数 758 浏览 1 评论 0原文

在我的 ASP.NET MVC 应用程序中,我想使用 这个 ASP.NET MVC 基于属性的路由映射器,首次公布 在这里

到目前为止,我了解如何实现使用它的代码,但我遇到了一些问题,我认为那些过去使用过这种基于属性的路由映射器的人将能够回答这些问题。

  • 如何将它与用于 HTTP POST 的 ActionResults 一起使用? 换句话说,它如何与表单提交等一起使用?我应该只放入 GET 方法的 URL,还是应该使用不带任何参数的 GET 方法 URL(如 HTTP POST 中所示)不是通过 URL 传入的)?
  • 如何将其与“URL 查询字符串参数”一起使用? 是否可以将该属性配置为映射到 /controller/action?id=value 等路由,而不是 /controller/action/{id}?

提前致谢。

In my ASP.NET MVC application, I want to use this ASP.NET MVC Attribute Based Route Mapper, first announced here.

So far, I understand how to implement code that uses it, but I've run into a few questions that I think those who have used this attribute-based route mapper in the past will be able to answer.

  • How do I use it with ActionResults that are for HTTP POSTs? In other words, how does it work with form submissions and the like? Should I just put the URL of the GET method in, or should I use the GET method URL without any parameters (as in HTTP POST they aren't passed in through the URL)?
  • How do I use it with "URL querystring parameters"? Can the attribute be configured to map to a route such as /controller/action?id=value rather than /controller/action/{id}?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淤浪 2024-09-13 07:04:28

如何将它与 ActionResults 一起使用
用于 HTTP POST 的?

您可以使用 [HttpPost] 属性来装饰要发布到的操作:

[Url("")]
public ActionResult Index() { return View(); }

[Url("")]
[HttpPost]
public ActionResult Index(string id) { return View(); }

如果您决定为 POST 操作指定不同的名称:

[Url("")]
public ActionResult Index() { return View(); }

[Url("foo")]
[HttpPost]
public ActionResult Index(string id) { return View(); }

您需要在辅助方法中提供此名称:

<% using (Html.BeginForm("foo", "home", new { id = "123" })) { %>

如何将它与“URL 查询字符串”一起使用
参数”?

查询字符串参数不是路由定义的一部分。您始终可以在控制器操作中作为操作参数或从 Request.Params 获取它们。

只要 id 参数涉及它在 Application_Start 中配置,因此如果您希望它出现在查询字符串中而不是成为路由的一部分,只需将其从该路由定义中删除即可:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoutes();
    routes.MapRoute(
        "Default",
        "{controller}/{action}",
        new { controller = "Home", action = "Index" }
    );
}

How do I use it with ActionResults
that are for HTTP POSTs?

You decorate the action that you are posting to with the [HttpPost] attribute:

[Url("")]
public ActionResult Index() { return View(); }

[Url("")]
[HttpPost]
public ActionResult Index(string id) { return View(); }

If you decide to give the POST action a different name:

[Url("")]
public ActionResult Index() { return View(); }

[Url("foo")]
[HttpPost]
public ActionResult Index(string id) { return View(); }

You need to supply this name in your helper methods:

<% using (Html.BeginForm("foo", "home", new { id = "123" })) { %>

How do I use it with "URL querystring
parameters"?

Query string parameters are not part of the route definition. You can always obtain them in a controller action either as action parameter or from Request.Params.

As far as the id parameter is concerned it is configured in Application_Start, so if you want it to appear in the query string instead of being part of the route simply remove it from this route definition:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoutes();
    routes.MapRoute(
        "Default",
        "{controller}/{action}",
        new { controller = "Home", action = "Index" }
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文