-
-
Save yaohuang/6ed0d6f74a6889ec0c3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// New code // | |
[AttributeUsage(AttributeTargets.Method)] | |
public class ResponseStatusAttribute : Attribute | |
{ | |
public ResponseStatusAttribute(params HttpStatusCode[] statusCodes) | |
{ | |
StatusCodes = statusCodes; | |
} | |
public HttpStatusCode[] StatusCodes { get; set; } | |
} | |
////////////// | |
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The exception is recorded as ErrorMessages.")] | |
private static HelpPageApiModel GenerateApiModel(ApiDescription apiDescription, HelpPageSampleGenerator sampleGenerator) | |
{ | |
HelpPageApiModel apiModel = new HelpPageApiModel(); // Edit HelpPageApiModel to add new properties | |
apiModel.ApiDescription = apiDescription; | |
// New code // | |
ResponseStatusAttribute customAttribute = apiDescription.ActionDescriptor.GetCustomAttributes<ResponseStatusAttribute>().FirstOrDefault(); | |
if (customAttribute != null) | |
{ | |
apiModel.StatusCodes = customAttribute.StatusCodes; | |
} | |
////////////// | |
try | |
{ | |
foreach (var item in sampleGenerator.GetSampleRequests(apiDescription)) | |
{ | |
apiModel.SampleRequests.Add(item.Key, item.Value); | |
LogInvalidSampleAsError(apiModel, item.Value); | |
} | |
foreach (var item in sampleGenerator.GetSampleResponses(apiDescription)) | |
{ | |
apiModel.SampleResponses.Add(item.Key, item.Value); | |
LogInvalidSampleAsError(apiModel, item.Value); | |
} | |
} | |
catch (Exception e) | |
{ | |
apiModel.ErrorMessages.Add(String.Format(CultureInfo.CurrentCulture, "An exception has occurred while generating the sample. Exception Message: {0}", e.Message)); | |
} | |
return apiModel; | |
} | |
// Add something like the following to HelpPageApiModel.cshtml // | |
@if (Model.StatusCodes != null) | |
{ | |
<h2>Status Codes</h2> | |
<ul> | |
@foreach (var status in Model.StatusCodes) | |
{ | |
<li>@status</li> | |
} | |
</ul> | |
} | |
////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment