1

I am using a custom action filter on my controller action.

My controller action is like this:

    [HttpPost]
    [Route("listener")]   
    [MyAttr]     
    public IHttpActionResult Listener([FromBody]Parameters request)
    {
        return Ok();
    }

I want to access Route("listener") values from action filter.

public class MyAttr: ActionFilterAttribute
{        
    public async override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
    {
         var route = actionExecutedContext.ActionContext.RequestContext.RouteData;       
    }
}

But RouteData values collection has no items. How can access route value?

My configuration is like this:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
5
  • Its not route data, route data can only be supplied in the URL and your method has data supplied in the http body. Commented Jul 8, 2017 at 12:09
  • How can I access "listener" of Route attribute parameter? Commented Jul 8, 2017 at 12:11
  • @bookmarker Do you want to access the URI the request is coming to? Commented Jul 8, 2017 at 12:13
  • You could access its attributes, try actionExecutedContext.ActionContext.ActionDescriptor.GetCustomAttributes<Route>() Commented Jul 8, 2017 at 12:13
  • and then you can access its Template property msdn.microsoft.com/en-us/library/… Commented Jul 8, 2017 at 12:18

1 Answer 1

0

Please add /{action} in your WebApiConfig.cs file.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

api/ControllerName/Listener the link will to be that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.