Skip to content

Instantly share code, notes, and snippets.

@karbyninc
Last active May 16, 2016 19:28
Show Gist options
  • Save karbyninc/588d8fd76b5d290820af to your computer and use it in GitHub Desktop.
Save karbyninc/588d8fd76b5d290820af to your computer and use it in GitHub Desktop.
public class EntityController : ApiController
{
public SomeDto Get()
{
//return some DTO
}
}
[HttpPost]
public void DoSomething(FooDto foo)
{
//....
}
POST http://localhost:5430/api/Entity
Headers:
Accept-language: application/json
Body:
{"a":"b"}
{"Message":"The request entity's media type 'text/plain' is not supported for this resource.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'FooDto' from content with media type 'text/plain'.","ExceptionType":"System.Net.Http.UnsupportedMediaTypeException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)rn at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)rn at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"}
$.postJSON = function(url, data, callback) {
return jQuery.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
'type': 'POST',
'url': url,
'data': JSON.stringify(data),
'dataType': 'json',
'success': callback
});
};
[HttpGet]
public SomeDto A()
{
//return some DTO
}
[HttpPost]
public SomeDto B()
{
//return some DTO
}
public void Blah()
{
var x = 1;
}
[RoutePrefix("api/foobar")]
public class EntityController : ApiController
{
public SomeDto Get()
{
//return some DTO
}
}
[RoutePrefix("api/foobar")]
public class EntityController : ApiController
{
[HttpGet, Route("")]
public SomeDto Get()
{
//return some DTO
}
}
public class EntityController : ApiController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace mypoc
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
[HttpPut, Route("foo/{field:Int}")]
public IHttpActionResult Foo(int testField)
{
return Ok();
}
[HttpPut, Route("foo/{field:Int}")]
public IHttpActionResult Foo(int field)
{
return Ok();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment