3

You can decorate a controller or action method with the ApiExplorerSettingsAttribute setting the IgnoreApi property to NOT generate help info. If you try to apply the same to an action method's attribute, you get an error:

public HttpResponseMessage Post([ApiExplorerSettings(IgnoreApi = true)]HttpRequestMessage request, ... )

Error 2 Attribute 'ApiExplorerSettings' is not valid on this declaration type. It is only valid on 'class, method' declarations.

A common convention for keeping your controller actions testable is to accept an HttpRequestMessage parameter, but this is an implementation detail, not something your API consumers should know about.

How can I prevent the ApiExplorer from including this parameter when it generates the help page?

Dzejms
  • 3,108
  • 2
  • 30
  • 40

1 Answers1

2

For clarity...by this "if you try to apply the same to an action method's attribute", did you mean you were trying to apply to the parameter?

-A quick fix would be is to add an additional check to the existing CancellationToken check that we have in this file: \Areas\HelpPage\Views\Help\DisplayTemplates\Parameters.cshtml

// Don't show CancellationToken because it's a special parameter
    if (!typeof(CancellationToken).IsAssignableFrom(parameter.ParameterDescriptor.ParameterType))
    {

-Also, you could avoid having HttpRequestMessage as a parameter on action as you can get the current request from the Request property on the controller, but you wanted it as a parameter for testing...is it?

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • "... trying to apply to the parameter", that is precisely what I meant. I've added an example to the question. Your last point is accurate on both counts. The Request property is available, but I like the explicit request parameter for testing. Also your work around works perfectly and is quite acceptable. Thank you so much for taking time to answer questions on SO. It's really helpful to have someone from the team who really knows this stuff inside and out. :) – Dzejms May 21 '13 at 13:14