45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
|
|
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 NX.ERP.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 配置和服务
|
|||
|
|
|
|||
|
|
// Web API 路由
|
|||
|
|
config.MapHttpAttributeRoutes();
|
|||
|
|
var jsonFormatter = new JsonMediaTypeFormatter();
|
|||
|
|
|
|||
|
|
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
|
|||
|
|
config.Routes.MapHttpRoute(
|
|||
|
|
name: "DefaultApi",
|
|||
|
|
routeTemplate: "api/{controller}/{action}/{id}",
|
|||
|
|
defaults: new { id = RouteParameter.Optional }
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|