我已经配置了Web API帮助页面文档.
我正在下面的类,它将在其他模型类中继承.
public class ResponseBase { public string ErrorReason { get; set; } [IgnoreDataMember] public bool IsRejected { get; set; } }
我不希望IsRejected被序列化并在响应中可用,因此我使用IgnoreDataMember属性对其进行了修饰.
继承ResponseBase的模型类示例.
public class Reading : ResponseBase { //Other properties }
我的Web API控制器中有以下方法:
[HttpPost] [ValidationResponseFilter] [Route("")] [ResponseType(typeof(Reading))] public IHttpActionResult Add(List<Reading> readingList) { //Logic here }
现在,对于作为Reading对象列表的请求参数的文档,它将列出Reading的所有属性以及ResponseBase类(继承).我希望帮助页面文档将ErrorReason列为仅响应中的参数之一.
除了在ErrorReason上设置属性[ApiExplorerSettings(IgnoreApi = true)]之外,我还能做任何配置吗?如果我这样做,ErrorReason会
请求和响应参数中都不可用.我想只在响应参数列表中显示它.
解决方法
通常,最佳做法是创建一个与您要接受的内容完全匹配的自定义对象.其中一个原因是安全性.请参阅“过度发布”和“发布不足”:
http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api
为此,您将创建一个仅包含所需属性的新对象.
为此,您将创建一个仅包含所需属性的新对象.
public class ResponseBaseVM { public string ErrorReason { get; set; } /*public bool IsRejected { get; set; }*/ } public class ReadingVM : ResponseBaseVM { //Other properties that you only want available to user }
然后你会接受ReadingVM列表,但响应类型仍然是typeof(阅读)
[HttpPost] [ValidationResponseFilter] [Route("")] [ResponseType(typeof(List<Reading>))] //will still display response with IsRejected public IHttpActionResult Add(List<ReadingVM> readingListVM) { //Logic here }
—工作—
我再次认为你应该遵循上面的方法.你要求解决这个问题.在HelpPageConfigurationExtensions.cs类中找到’GenerateApiModel’方法并使用以下方法重新生成它:
private static HelpPageApiModel GenerateApiModel(ApiDescription apiDescription,HttpConfiguration config) { HelpPageApiModel apiModel = new HelpPageApiModel() { ApiDescription = apiDescription,}; ModelDescriptionGenerator modelGenerator = config.GetModelDescriptionGenerator(); HelpPageSampleGenerator sampleGenerator = config.GetHelpPageSampleGenerator(); GenerateUriParameters(apiModel,modelGenerator); GenerateRequestModelDescription(apiModel,modelGenerator,sampleGenerator); GenerateResourceDescription(apiModel,modelGenerator); GenerateSamples(apiModel,sampleGenerator); //This will remove request body parameters from your Api Help Page matching 'IsRejected' var isRejectedParameter = apiModel.RequestBodyParameters.SingleOrDefault(x => x.Name == "IsRejected"); if (isRejectedParameter != null) apiModel.RequestBodyParameters.Remove(isRejectedParameter); //This will remove elements with 'IsRejected' for the Help Page sample requests var sampleRequests = new Dictionary<MediaTypeHeaderValue,object>(); foreach (var kvp in apiModel.SampleRequests) { //1.) iterate through each object in SampleRequests dictionary. //2.) modify the json or xml to remove the "IsRejected" elements //3.) assign modified results to a new dictionary //4.) change the HelpPageApiModel. SampleRequests setter to be not private //5.) assign new dictionary to HelpPageApiModel.SampleRequests if (Equals(kvp.Key,new MediaTypeHeaderValue("application/json"))) { var jObject = JObject.Parse(kvp.Value.ToString()); jObject.Remove("IsRejected"); sampleRequests.Add(new MediaTypeHeaderValue("application/Json"),jObject.ToString()); } else if(Equals(kvp.Key,new MediaTypeHeaderValue("text/json"))) { //do stuff } else if (Equals(kvp.Key,new MediaTypeHeaderValue("application/xml"))) { //do stuff } else if (Equals(kvp.Key,new MediaTypeHeaderValue("text/xml"))) { //do stuff } else { //form urlencoded or others sampleRequests.Add(kvp.Key,kvp.Value); } } apiModel.SampleRequests = sampleRequests; return apiModel; }