Skip to content

Instantly share code, notes, and snippets.

View AlexZeitler's full-sized avatar
👷‍♂️
Building stuff

Alexander Zeitler AlexZeitler

👷‍♂️
Building stuff
View GitHub Profile
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var builder = new ContainerBuilder();
builder.Register<ICustomerRepository, CustomerRepository>();
var container = builder.Build();
var config = GlobalConfiguration.Configuration;
config.ServiceResolver.SetResolver(
t => container.Resolve(t),
t => container.ResolveAll(t));
public class Customer {
public int Id { get; set; }
[Required]
public string Name { get; set; }
}
public class ValidationActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext context)
{
var modelState = context.ModelState;
if (!modelState.IsValid)
{
dynamic errors = new JsonObject();
foreach (var key in modelState.Keys)
{
$.ajax({
url: '/api/customers',
cache: false,
type: 'POST',
data: json,
contentType: 'application/json; charset=utf-8',
statusCode: {
201 /*Created*/: function (data) {
viewModel.customers.push(data);
},
<label for="customer">Customer</label>
<input id="customer" name="customer" type="text" data-val="true" data-val-required="Kunden-Name benötigt" value="" />
<span class="field-validation-valid" data-valmsg-for="customer" data-valmsg-replace="true"></span>
static void Main()
{
var config = new HttpSelfHostConfiguration("http://localhost:9191");
config.Routes.MapHttpRoute("default", "{controller}/{id}",
new { id = UrlParameter.Optional });
var builder = new ContainerBuilder();
builder.Bind<ICustomerRepository, CustomerRepository>();
/// <summary>
/// This filter will manage the session for all of the controllers that needs a Raven Document Session.
/// It does so by automatically injecting a session to the first public property of type IDocumentSession available
/// on the controller.
/// </summary>
public class RavenActionFilterAttribute : ActionFilterAttribute {
public override void OnActionExecuting(HttpActionContext filterContext) {
filterContext.Request.Properties["RavenDocumentStore"] =
DocumentStoreHolder.TryAddSession(filterContext.ControllerContext.Controller);
/// <summary>
/// This class manages the state of objects that desire a document session. We aren't relying on an IoC container here
/// because this is the sole case where we actually need to do injection.
/// </summary>
public class DocumentStoreHolder {
private static IDocumentStore documentStore;
public static IDocumentStore DocumentStore {
get { return (documentStore ?? (documentStore = CreateDocumentStore())); }
}
public class RavenController : ApiController {
public new IDocumentSession Session { get; set; }
}