Skip to content
This repository has been archived by the owner on Feb 20, 2021. It is now read-only.

Latest commit

 

History

History

ASP.NET Web API Sending Form-Url-Encoded Data

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

ASP.NET Web API: Sending Form-Url-Encoded Data

Requires

  • Visual Studio 2012

License

  • Apache License, Version 2.0

Technologies

  • ASP.NET

Topics

  • ASP.NET Web API

Updated

  • 09/21/2012

Description

Sample code for the tutorial at http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1

 [Excerpt]

Part 1: Form-urlencoded Data

This article shows how to post form-urlencoded data to a Web API controller.

Overview of HTML Forms

Forms use either GET or POST to send data to the server. The method attribute of the form element gives the HTTP method:

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

The default method is GET. Using GET, the form data is encoded in the URI as a query string. Using POST, the form data is placed in the request body, and the enctype attribute specifies the format.

  •  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.
  • multipart/form-data: Form data is encoded as a multipart MIME message. Use this format if you are uploading a file to the server.

This article looks at POST with application/x-www-form-urlencoded encoding.

 

Sending Complex Types

Typically, you will send a complex type, composed of values from several form controls. Consider the following model:

namespace FormEncode.Models
{
    using System;
    using System.ComponentModel.DataAnnotations;
public class Update
{
    [Required]
    [MaxLength(140)]
    public string Status { get; set; }

    [Required]
    public DateTime Date { get; set; }
}

}

The Update class represents a status update. Here is a Web API controller that accepts an Update object via POST.

namespace FormEncode.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using FormEncode.Models;
public class UpdatesController : ApiController
{
    static readonly Dictionary&lt;Guid, Update&gt; updates = new Dictionary&lt;Guid,Update&gt;();

    [HttpPost]
    [ActionName(&quot;Complex&quot;)]
    public HttpResponseMessage PostComplex(Update update)
    {
        if (ModelState.IsValid)
        {
            var id = Guid.NewGuid();
            updates[id] = update;
            return ResponseFromPost(update, id);
        }
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }

    [HttpGet]
    public Update Status(Guid id)
    {
        Update update;
        if (updates.TryGetValue(id, out update))
        {
            return update;
        }
        else
        {
            var resp = Request.CreateResponse(HttpStatusCode.NotFound);
            throw new HttpResponseException(resp);
        }
    }

    // Create a 201 response for a POST action.
    private HttpResponseMessage ResponseFromPost(Update update, Guid id)
    {
        var resp = new HttpResponseMessage(HttpStatusCode.Created);
        resp.Content = new StringContent(update.Status);
        resp.Headers.Location = 
            new Uri(Url.Link(&quot;DefaultApi&quot;, new { action = &quot;status&quot;, id = id }));
        return resp;
    }
}

}