Skip to content

Instantly share code, notes, and snippets.

@scottmcarthur
Created April 29, 2014 09:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottmcarthur/11394855 to your computer and use it in GitHub Desktop.
Save scottmcarthur/11394855 to your computer and use it in GitHub Desktop.
Sending complex data to ServiceStack from HTML form synchronously. (ServiceStack v4)
using System;
using ServiceStack;
using ServiceStack.Web;
using ServiceStack.Text;
namespace v4
{
class MainClass
{
public static void Main()
{
var appHost = new AppHost(500);
appHost.Init();
appHost.Start("http://*:9000/");
Console.ReadKey();
}
}
public class AppHost : AppHostHttpListenerPoolBase
{
public AppHost(int poolSize) : base("Test Service", poolSize, typeof(TestService).Assembly)
{
}
public override void Configure(Funq.Container container)
{
}
}
public class GetFromJsonVariableAttribute : Attribute, IHasRequestFilter
{
string _variableName;
public GetFromJsonVariableAttribute(string variableName = "Data")
{
_variableName = variableName;
}
public void RequestFilter(IRequest req, IResponse res, object requestDto)
{
// Convert the JSON payload to DTO format
var payload = req.GetParam(_variableName);
if(payload != null)
requestDto = JsonSerializer.DeserializeFromString(payload, requestDto.GetType());
}
public int Priority { get { return int.MinValue; } }
IHasRequestFilter IHasRequestFilter.Copy()
{
return this;
}
}
[GetFromJsonVariable("Data")]
[Route("/Customers","POST")]
public class CreateCustomerRequest : IReturnVoid
{
public Customer Customer { get; set; }
public Name Name { get; set; }
}
public class Customer
{
public string Company { get; set; }
public string RegionCode { get; set; }
}
public class Name
{
public string First { get; set; }
public string Last { get; set; }
}
public class TestService : Service
{
public void Post(CreateCustomerRequest request)
{
// request will be populated as expected with the complex JSON
// Redirect the user to next page
Response.Redirect("/Success.html");
Response.EndRequest();
// or return an html response
Response.ContentType = "text/html";
Response.WriteFile("Success.html");
}
}
}
<html>
<head>
<title>ServiceStack Send Complex Form (Synchronously)</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#CreateCustomer").on("submit", function(){
// Get the form values into simple key value array
var values = {};
$.each($(this).serializeArray(), function(){ values[this.name] = this.value; });
// Prepare the DTO
var data = {
Customer: {
Company: values["Company"],
RegionCode: values["RegionCode"]
},
Name: {
First: values["First"],
Last: values["Last"]
}
};
// Convert it to JSON
$('#PayloadForm [name="Data"]').val(JSON.stringify(data));
$('#PayloadForm').submit();
return false;
});
});
</script>
<style>
label { display: inline-block; width: 100px; text-align: right; margin-right: 20px; }
</style>
</head>
<body>
<h1>Create Customer</h1>
<form id="CreateCustomer">
<h2>Customer</h2>
<label>Company</label><input type="text" name="Company" value="TheCompany" /><br/>
<label>Region Code</label><input type="text" name="RegionCode" value="AU_NSW" /><br/>
<h2>Name</h2>
<label>First</label><input type="text" name="First" value="Jimi" /><br/>
<label>Last</label><input type="text" name="Last" value="Hendrix" /><br/>
<br/>
<input type="submit" value="Submit" />
</form>
<!-- This form is hidden -->
<form action="/Customers" method="POST" id="PayloadForm"><input type="hidden" name="Data" value=""></form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment