5.2 发送HTML表单数据:URL编码的表单数据

本文是【ASP.NET Web API系列教程】的一部分,如果您是第一次看本系列教程,请先看前面的内容。

5.2 Sending HTML Form Data
5.2 发送HTML表单数据

本文引自:http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1

By Mike Wasson|June 15, 2012
作者:Mike Wasson | 日期:2012-6-15

Part 1: Form-urlencoded Data
第1部分:URL编码的表单数据

This article shows how to post form-urlencoded data to a Web API controller.
本文显示如何向Web API控制器递交URL编码的表单数据。

  • Overview of HTML Forms
    HTML表单概述
  • Sending Complex Types
    发送复合类型
  • Sending Form Data via AJAX 
    通过AJAX发送表单数据
  • Sending Simple Types
    发送简单类型

Download the completed project.
下载完整的项目。

Overview of HTML Forms
HTML表单概述

HTML forms use either GET or POST to send data to the server. The method attribute of the form element gives the HTTP method:
HTML表单使用GET或POST将数据发送给服务器。form元素的method标签属性给出HTTP方法:

<form action="api/values" method="post">

The default method is GET. If the form uses GET, the form data is encoded in the URI as a query string. If the form uses POST, the form data is placed in the request body. For POSTed data, the enctype attribute specifies the format of the request body:
默认方法是GET。如果form使用GET,表单数据作为查询字符串被编码到URI中。如果form使用POST,表单数据被放在请求体中。对于POST的数据,enctype标签属性会指明请求体的格式:

enctypeDescription
描述
application/x-www-form-urlencoded Form data is encoded as name/value pairs, similar to a URI query string. This is the default format for POST.
表单数据被编码成“名字/值”对,类似于URI查询字符串。这是POST的默认格式。
multipart/form-data Form data is encoded as a multipart MIME message. Use this format if you are uploading a file to the server.
表单数据被编码成多部分MIME消息。如果把文件上传到服务器,使用的是这种格式。

MIME指Multipurpose Internet Mail Extensions — 多用途互联网邮件扩展,它是通过网络传递邮件消息的一个互联网标准。MIME规定了用于表示各种数据类型的符号化方法。在HTTP协议中,对HTTP消息的内容类型也采用了MIME的这种表示数据类型的方法。上述enctype标签属性意为“编码类型”,就是用来指定HTTP消息的Content-Type(内容类型)报头属性。给这个标签属性所指定的值必须是MIME对Content-Type所规定的值之一。上表中便是MIME中关于内容类型的其中两个值。更多内容请参阅MIME的有关资料 — 译者注

Part 1 of this article looks at x-www-form-urlencoded format. Part 2 describes multipart MIME.
本文的第1部分考察x-www-form-urlencoded格式。第2部分描述多部分MIME。

Sending Complex Types
发送复合类型

Typically, you will send a complex type, composed of values taken from several form controls. Consider the following model that represents a status update:
典型地,你要发送的是一个复合类型,它由几个表单控件的值所组成。考虑以下表示状态更新的一个模型:

namespace FormEncode.Models 

using System; 
using System.ComponentModel.DataAnnotations;

public class Update 

[Required] 
[MaxLength(140)] 
public string Status { get; set; } 

public DateTime Date { get; set; } 

}

Here is a Web API controller that accepts an Update object via POST.
以下是通过POST接收Update对象的一个Web API控制器。

namespace FormEncode.Controllers 

using FormEncode.Models; 
using System; 
using System.Collections.Generic; 
using System.Net; 
using System.Net.Http; 
using System.Web; 
using System.Web.Http; 

public class UpdatesController : ApiController 

static readonly Dictionary<Guid, Update> updates = new Dictionary<Guid, Update>(); 

[HttpPost] 
[ActionName("Complex")] 
public HttpResponseMessage PostComplex(Update update) 

if (ModelState.IsValid && update != null) 

// Convert any HTML markup in the status text.
// 转换status文本中的HTML标记。
update.Status = HttpUtility.HtmlEncode(update.Status); 

// Assign a new ID.
// 赋一个新的ID。
var id = Guid.NewGuid(); 
updates[id] = update; 

// Create a 201 response. 
// 创建一个201响应。
var response = new HttpResponseMessage(HttpStatusCode.Created) 

Content = new StringContent(update.Status) 
}; 
response.Headers.Location =
new Uri(Url.Link("DefaultApi", new { action = "status", id = id })); 
return response; 

else 

return Request.CreateResponse(HttpStatusCode.BadRequest); 



[HttpGet] 
public Update Status(Guid id) 

Update update; 
if (updates.TryGetValue(id, out update)) 

return update; 

else 

throw new HttpResponseException(HttpStatusCode.NotFound); 



}

This controller uses action-based routing, so the route template is "api/{controller}/{action}/{id}". The client will post the data to "/api/updates/complex".
这个控制器使用了“基于动作的路由(本系列教程的第4.1小节 — 译者注)”,因此,路由模板是“api/{controller}/{action}/{id}”。客户端会把这些数据递交给“/api/updates/complex”。

Now let’s write an HTML form for users to submit a status update.
现在,让我们编写一个用户递交状态更新的HTML表单。

<h1>Complex Type</h1> 
<form id="form1" method="post" action="api/updates/complex"
enctype="application/x-www-form-urlencoded"> 

&l
posted on 2013-06-27 18:56  青峰123  阅读(298)  评论(0编辑  收藏  举报

磁翻板液位计 上海中央空调