WebApi项目需要在输出Json时对Json进行编码处理。考虑使用MVC 的AOP
查阅文档需要HttpMessageHandle
新建类CryptDelegatingHandler
public class CryptDelegatingHandler:DelegatingHandler
{
protected async override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
bool jsonNeedCrypt =Convert.ToBoolean(FastFoodMvcApi.Common.ConfigList.GetValueFromConfig("JsonNeedCrypt"));
if (jsonNeedCrypt)
response.Content =new StringContent(JsonCrypt.EncryptJsonString(response.Content.ReadAsStringAsync().Result));
//解密测试
//Console.Write(JsonCrypt.DecryptJsonString(response.Content.ReadAsStringAsync().Result));
return response;
}
}
加入WebApiConfig中
Register
config.MessageHandlers.Add(new CryptDelegatingHandler());
详细原理转http://www.cnblogs.com/r01cn/archive/2012/12/07/2807059.html