简体   繁体   中英

Web API 2.0 Dynamic routing

Suppose I have a file system that uses a dynamic folder structure for storing resources:

api/media/videos/file1.avi
api/media/pictures/pic1.png
api/documents/papers/math/paper1.txt

As you can see, there is a dynamic part on the url after the /api and the {filename} parameter:

/api/*/{filename}

I'm currently using WebApi 2.0 annotations:

[Route("api/data/{filename}")]

and alike. But it works only for 'harcoded' routes.

How can I achieve the 'dynamic' routing using these annotations?

Thanks!

Not sure if this is best practice or not but I've done this by using the Application_BeginRequest method in the global.asax file. You can use Context.RewritePath to map the incomming url to anything you like in your site. You just need to write the code to parse the incomming url and make the required modifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM