__Scenario__:
A user sometimes would like to keep some methods public in his controller but would not like it to be invoked by end users, so he decorates it with [NonAction] attribute.
__Issue__:
In the following scenario, a route is being added to the route collection:
```
[RoutePrefix("api/values")]
public class ValuesController : ApiController
{
[NonAction]
public IEnumerable<string> GetAllValues()
{
return new string[] { "value1", "value2" };
}
}
```
Following would be how the route could look like in the route collection:
config.Routes.MapHttpRoute("routeName", "api/values", new { }, new { }, new { actions = [Values.GetAllValues()] });
__Workaround__:
Create custom route builder deriving from the default one. Override one of the "BuildHttpRoute" methods which provides access to the action descriptor and using it you can check to see if there are any NonAction attributes decorated and if present, ignore adding the route to the route collection.
__UPDATE__:
I have tried the above workaround, but I am currently receiving an exception. __Should we allow returning null from route builder?__ I cannot think of other scenarios where users might want to do this.
```
public override IHttpRoute BuildHttpRoute(string routeTemplate, IEnumerable<HttpMethod> httpMethods,
IEnumerable<ReflectedHttpActionDescriptor> actions)
{
ReflectedHttpActionDescriptor nonActionDescriptor = actions.FirstOrDefault();
if (nonActionDescriptor != null)
{
Collection<NonActionAttribute> nonActionAttribs = nonActionDescriptor.GetCustomAttributes<NonActionAttribute>(inherit: false);
if (nonActionAttribs.Count > 0)
{
return null;
}
}
return base.BuildHttpRoute(routeTemplate, httpMethods, actions);
}
```
__Exception__:
System.ArgumentNullException occurred
HResult=-2147467261
Message=Value cannot be null.
Parameter name: route
Source=System.Web.Http
ParamName=route
StackTrace:
at System.Web.Http.HttpRouteCollection.Add(String name, IHttpRoute route) in c:\WebstackRuntime\Runtime\src\System.Web.Http\HttpRouteCollection.cs:line 167
InnerException:
A user sometimes would like to keep some methods public in his controller but would not like it to be invoked by end users, so he decorates it with [NonAction] attribute.
__Issue__:
In the following scenario, a route is being added to the route collection:
```
[RoutePrefix("api/values")]
public class ValuesController : ApiController
{
[NonAction]
public IEnumerable<string> GetAllValues()
{
return new string[] { "value1", "value2" };
}
}
```
Following would be how the route could look like in the route collection:
config.Routes.MapHttpRoute("routeName", "api/values", new { }, new { }, new { actions = [Values.GetAllValues()] });
__Workaround__:
Create custom route builder deriving from the default one. Override one of the "BuildHttpRoute" methods which provides access to the action descriptor and using it you can check to see if there are any NonAction attributes decorated and if present, ignore adding the route to the route collection.
__UPDATE__:
I have tried the above workaround, but I am currently receiving an exception. __Should we allow returning null from route builder?__ I cannot think of other scenarios where users might want to do this.
```
public override IHttpRoute BuildHttpRoute(string routeTemplate, IEnumerable<HttpMethod> httpMethods,
IEnumerable<ReflectedHttpActionDescriptor> actions)
{
ReflectedHttpActionDescriptor nonActionDescriptor = actions.FirstOrDefault();
if (nonActionDescriptor != null)
{
Collection<NonActionAttribute> nonActionAttribs = nonActionDescriptor.GetCustomAttributes<NonActionAttribute>(inherit: false);
if (nonActionAttribs.Count > 0)
{
return null;
}
}
return base.BuildHttpRoute(routeTemplate, httpMethods, actions);
}
```
__Exception__:
System.ArgumentNullException occurred
HResult=-2147467261
Message=Value cannot be null.
Parameter name: route
Source=System.Web.Http
ParamName=route
StackTrace:
at System.Web.Http.HttpRouteCollection.Add(String name, IHttpRoute route) in c:\WebstackRuntime\Runtime\src\System.Web.Http\HttpRouteCollection.cs:line 167
InnerException: