2

So I have registered the following route right now:

configuration.Routes.MapHttpRoute(
            name: "MediaHandler",
            routeTemplate: "api/mediahandler/{action}/{id}",
            defaults: new { controller = "PortalAsset", id = RouteParameter.Optional }
        );

And here is how the controller looks like:

 public class MediaHandlerController : ApiControllerBase
{
    ///...
      [HttpGet]
      [ActionName("download")]
      public async Task<HttpResponseMessage> DownloadAsset(long id)
      {
           // action
      }

I want to add boolean parameter to the controller - isPreview and want to map the routhe the following way:

  1. http://host/api/mediahandler/download/1893 maps to: id=1893, isPreview=false
  2. http://host/api/mediahandler/download/1893/preview maps to: id=1893, isPreview = true

Is there a way I can acomplish that?

3
  • You could do that with attribute routing. Any reason for choosing the default routing scheme over attribute routing? Commented Sep 14, 2016 at 13:04
  • @scheien the reason is that this is the single route scheme. By doing that by attributes I need to apply it to each action method or at least each controller. But for this case it might be what I need. Thanks for clue. Commented Sep 14, 2016 at 13:10
  • I see what you mean. You're welcome. Hope you'll sort it out. Commented Sep 14, 2016 at 13:22

1 Answer 1

3

With attribute routing you can do like this

[RoutePrefix("api/mediahandler/download")]
public class MediaHandlerController : ApiControllerBase
{
      [HttpGet]
      [Route("{id}")]
      public async Task<HttpResponseMessage> DownloadAsset(long id)
      {
           return DownloadAsset(id, false);
      }

      [HttpGet]
      [Route("{id}/preview")]
      public async Task<HttpResponseMessage> DownloadAssetPreview(long id)
      {
          return DownloadAsset(id, true);
      }

      private async Task<HttpResponseMessage> DownloadAsset(long id, bool isPreview)
      {
           // action
      }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You could put 'api/mediahandler' as a RoutePrefix attribute on the class to shorten the routes on the actions themselves.

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.