2017-06-12 12:46:00 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Formatting;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
|
|
|
|
|
namespace WebApi
|
|
|
|
|
{
|
|
|
|
|
public class JsonContentNegotiator : IContentNegotiator
|
|
|
|
|
{
|
|
|
|
|
private readonly JsonMediaTypeFormatter _jsonFormatter;
|
|
|
|
|
|
|
|
|
|
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
|
|
|
|
|
{
|
|
|
|
|
_jsonFormatter = formatter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
|
|
|
|
|
{
|
|
|
|
|
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class WebApiConfig
|
|
|
|
|
{
|
|
|
|
|
public static void Register(HttpConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
// Web API configuration and services
|
|
|
|
|
|
|
|
|
|
//DEBUG
|
|
|
|
|
config.EnableSystemDiagnosticsTracing();
|
|
|
|
|
|
|
|
|
|
// Web API routes
|
|
|
|
|
config.MapHttpAttributeRoutes();
|
|
|
|
|
|
|
|
|
|
config.Routes.MapHttpRoute(
|
|
|
|
|
name: "DefaultApi",
|
|
|
|
|
routeTemplate: "api/{controller}/{id}",
|
|
|
|
|
defaults: new { id = RouteParameter.Optional }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Force always json response
|
|
|
|
|
var jsonFormatter = new JsonMediaTypeFormatter();
|
|
|
|
|
//optional: set serializer settings here
|
2017-06-12 13:37:48 +03:00
|
|
|
|
//config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
|
2017-06-12 12:46:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|