ASP.NET MVC - 如何创建 RESTful 控制器方案?

发布于 2024-09-24 17:06:36 字数 2080 浏览 12 评论 0原文

如果这是重复的,请告诉我 - 因为我快速浏览了一下,找不到任何真正回答我的问题的内容。

我正在尝试 ASP.NET MVC 2。现在,我拥有 Web 窗体背景,我只真正处理过 HTTP GETHTTP POST

我正在尝试了解如何将 GET/PUT/POST/DELETE 应用于我的存储库上的相应 CRUD 操作(查找、插入、更新、删除)。

假设我有一个控制器,具有以下操作方法:

[HttpGet] // GET: /Products/{id}
[ActionName("Products")
public ActionResult Get(int id = 0) { ... }

[HttpPut] // PUT: /Products
[ActionName("Products")
public ActionResult Put(Product product) { ... }

[HttpPost] // POST: /Products/{product}
[ActionName("Products")
public ActionResult Post(Product product) { ... }

[HttpDelete] // DELETE: /Products/{product}
[ActionName("Products")
public ActionResult Delete(Product product) { .. }

有几个问题 - 您将如何命名/分隔操作方法?我应该传递整个模型(产品),还是只传递 id?

我遇到的问题是我不确定如何使用视图中的相关 HTTP 动词来处理调用这些操作方法。

在这个阶段,我想我会有 3 个视图:

  1. Index” - 绑定到 IEnumerable 模型,列出所有产品,带有“编辑”、“详细信息”和“删除”链接
  2. 单个”- 绑定到单个产品模型,列出产品的所有详细信息,并带有“更新”按钮。
  3. 新建” - 绑定到单个产品模型,带有用于创建产品的表单,带有“创建”按钮。

所以 - 我的问题是,我如何指定我想要使用特定的 HTTP 动词调用特定的控制器方法?

使用 Html.BeginForm,您可以指定 FormMethod 枚举 - 但它只有 GET 和 POST。

  • 如何执行 PUT 和 DELETE 命令?
  • 我需要为每个 HTTP 动词提供单独的视图吗?
  • 如果我有一个名为“Delete”的链接,我可以向我的控制器调用 HTTP DELETE 命令,还是需要使用表单操作删除将其重定向到新视图?

或者,这首先是一个愚蠢/矫枉过正的设计,我应该坚持使用“GET”和“POST”吗?

我对这种 Web 开发风格(REST)(相当)陌生,所以请友善。 :)

更新

所以我遇到了这篇来自 Stephen Walther 的有趣文章,涉及这个主题。

他表示 HTML 表单仅支持 GET 和 POST(因为我是 REST 风格的 Web 开发新手,我什至不知道这一点,对此我感到有些羞愧)。

显然,使用 PUT/DELETE 调用控制器操作的唯一方法是使用 AJAX。严重地?

那么我应该在这里做什么,我应该坚持使用 GET/POST,还是应该创建一个 JavaScript 文件,将底层 XmlHttpRequest 代码包装在一个不错的函数后面?

ASP.NET MVC 开发人员倾向于哪种方式?肯定有人问过自己同样的问题。

If this is a duplicate, please let me know - because i had a quick look and couldn't find anything that really answers my question.

I'm experimenting with ASP.NET MVC 2. Now coming from a Web Forms background, i only really dealt with HTTP GET and HTTP POST.

I'm trying to see how i could apply GET/PUT/POST/DELETE to the respective CRUD operations on my repository (Find, Insert, Update, Remove).

Say i have a single controller, with the following action methods:

[HttpGet] // GET: /Products/{id}
[ActionName("Products")
public ActionResult Get(int id = 0) { ... }

[HttpPut] // PUT: /Products
[ActionName("Products")
public ActionResult Put(Product product) { ... }

[HttpPost] // POST: /Products/{product}
[ActionName("Products")
public ActionResult Post(Product product) { ... }

[HttpDelete] // DELETE: /Products/{product}
[ActionName("Products")
public ActionResult Delete(Product product) { .. }

Couple of questions on that - is that how you would name/separate the action methods? Should i be passing through the entire model (Product), or just the id?

The problem i'm having is i'm not sure how to handle invoking these action methods with the relevant HTTP Verb in my View.

At this stage, I'm thinking i would have 3 Views:

  1. "Index" - bind to IEnumerable<Product> model, listing all products, with "Edit", "Details" and "Delete" links
  2. "Single" - bind to single Product model, listing all details for a product, with an "Update" button.
  3. "New" - bind to single Product model, with form for creating product, with an "Create" button.

So - my question is, how do i specify i want to invoke a particular controller method with a specific HTTP Verb?

With Html.BeginForm, you can specify a FormMethod enumeration - but it only has GET and POST.

  • How can i perform a PUT and DELETE command?
  • Will i need a seperate View for each HTTP Verb?
  • If i have a link called "Delete", can i invoke a HTTP DELETE command to my controller, or does it need to be redirected to a new View with the form action delete?

Or, is this a silly/overkill design in the first place, should i just stick with "GET" and "POST"?

I'm (fairly) new to this style of web development (REST), so please be kind. :)

UPDATE

So i came across this interesting article from Stephen Walther, regarding this very topic.

He indicates a HTML Form only supports GET and POST (because i'm new to REST-style web development, i did not even know this, to which i am partially ashamed).

Apparently the only way to invoke a Controller action with PUT/DELETE is to do it with AJAX. Seriously?

So what should i do here, should i stick with GET/POST, or should i create a JavaScript file which wraps the underlying XmlHttpRequest code behind a nice function?

Which way are ASP.NET MVC developers leaning? Surely someone has asked themselves this same question.

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

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

发布评论

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

评论(3

腻橙味 2024-10-01 17:06:37

就我个人而言,我坚持尽可能简单的完整请求,而不是全部使用 AJAX。我使用 ajax,但在特定的地方它可以增强体验(我使用渐进式 javascript 方法)。

由于上述原因,我总是只使用 GET 和 POST。

至于命名,我会这样做:获取、更新、创建、删除,这非常清楚。实际上,我使用的更像是与操作的操作相关的操作名称,但对于简单的事情,这些工作正常。

Personally I stick to as simple as possible full requests, instead of going AJAX all over. I use ajax, but in specific places where it enhances the experience (and I do with a progressive javascript approach).

Because of the above, I always just use GET and POST.

As for the naming, for those I'd do: Get, Update, Create, Delete, which makes it pretty clear. I actually use more like an action name that's related to what the operation does, but for simple things those work fine.

北渚 2024-10-01 17:06:36

您最熟悉 GET 和 POST 的原因是因为常规的旧浏览器只能 GET 或 POST URL,尤其是在提交表单时。

以这种方式设置控制器,您至少在精神上会看到更多类似于 REST API 与网站的东西。

是的,您是正确的,只有 AJAX 请求可以显式设置其请求方法,因此,如果您要执行诸如通过 AJAX 删除产品之类的操作,那么您就可以开始了(在这种情况下,我建议只传递 ID,因为它是比序列化整个产品轻得多,并且您可能可以轻松访问标记中的 ID)。

事情是这样的。如果您正在构建一个现代 Web 应用程序,您可能正在使用 AJAX,并且不要过于教条,您应该期望您的用户拥有 JavaScript。如果您想在不使用 AJAX 的情况下进行 CRUD,那么您始终可以 POST 产品或使用 GET 来删除 URL 中的 ID 方法(需要注意的是,请确保删除方法受到授权保护,否则网络爬虫会很乐意使用您的GET 并删除所有数据...)

如果您不想构建 REST API,那么不必担心您没有使用 DELETE(只需使用 GET 和 URL 中的 ID),否则您就不使用 PUT(只需将 POST 与 POST 正文中的产品实体一起使用)。

REST 是通过 HTTP 构建 API 的绝佳风格(无表示层,数据以原始格式发送给使用它们的客户端,可以是电话或网站等)。 HTTP 本身对于构建网页来说非常有用。用你所需要的,为你所需要的。

如果您想构建一个可供其他人和您的网站使用的 REST API,那么就这样做,然后让您网站的控制器操作方法直接调用您的 API 方法。我一直这样做。使用 Hammock 等网络客户端可以让您更轻松。

The reason that you're mostly familiar with GET and POST is because a regular old browser can only GET or POST a URL, especially when submitting a form.

Setting up your controllers this way, you're looking at something more along the lines of a REST API vs. a web site at least in spirit.

And yes, you are correct that only AJAX requests can set their request methods explicitly, so you're good to go if you'll be performing actions like deleting products via AJAX (in which case I would suggest only passing the ID since it is much lighter than serializing the entire product and you'll likely have easy access to the ID in your markup).

Here's the thing. If you are building a modern web application you're probably using AJAX and without getting too dogmatic you should expect your users to have JavaScript. If you want to do CRUD without AJAX then you can always POST a Product or use GET for a delete method with the ID in the URL (caveat to that, make sure the Delete method is protected behind authorization or a web crawler will gladly consume your GETs and delete all your data...)

If you don't want to build a REST API then don't sweat the fact that you're not using DELETE (just use GET with the ID in the URL) or you're not using PUT (just use POST with the product entity in the POST body).

REST is a great style for building APIs (no presentation tier, data sent in raw format to clients who consume them, could be a phone or a website, etc.) over HTTP. HTTP is great on its own for building web pages. Use what you need for what you need it for.

If you want to build a REST API to be used by both other people AND your web site, then do that, and just have your site's controller action methods call your API methods directly. I do that all the time. Use a web client like Hammock to make it easier on you.

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