使用Web API动态路由 [英] Dynamic Routing with Web API
问题描述
我有一个get方法的WebAPI控制器如下:
公共类myController的:ApiController
{
公众的ActionResult获取(字符串ID)
{
//做一些东西
}
}
目前的挑战是,我试图使用Web API来实现的WebDAV。这意味着,当用户浏览下来的文件夹结构中的URL会变成这样的:
/api/MyController/ParentFolder1/ChildFolder1/item1.txt
块引用>有没有办法来路由的行动MyController.Get并提取出道路让我得到:
ParentFolder1 / ChildFolder1 / item1.txt
块引用>谢谢!
解决方案动态的路线是没有问题的。只需使用通配符:
config.Routes.MapHttpRoute(
名称:NavApi
routeTemplate:API /我的/ {* ID}
默认:新{控制器=我}
);这条路线应前默认的添加。
问题是,要结束与URL文件扩展名。这将是PTED为静态请求.TXT文件间$ P $。
在IIS7 +,你可以解决,通过在web.config中添加一行:< system.webServer>
<模块runAllManagedModulesForAllRequests =真/>的不要忘记,如果你使用
myController的
,然后航段只不过是我的的I have a WebAPI controller with a Get method as follows:
public class MyController : ApiController { public ActionResult Get(string id) { //do some stuff } }
The challenge is that I am attempting to implement WebDAV using Web API. What this means is that as a user browses down a folder structure the URL will change to something like:
/api/MyController/ParentFolder1/ChildFolder1/item1.txt
Is there a way to route that action to MyController.Get and extract out the path so that I get:
ParentFolder1/ChildFolder1/item1.txt
Thanks!
解决方案"Dynamic" route is not a problem. Simply use wildcard:
config.Routes.MapHttpRoute( name: "NavApi", routeTemplate: "api/my/{*id}", defaults: new { controller = "my" } );
This route should be added before default one.
Problem is that you want to end URL with file extension. It will be interpreted as static request to .txt file. In IIS7+ you can work around that by adding line in web.config:
<system.webServer> <modules runAllManagedModulesForAllRequests="true" />
Don't forget that if you use
MyController
, then route segment is just "my"这篇关于使用Web API动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!