-1

我想创建一个API控制一个自定义路由,具有以下结构:ASP.NET API控制方法的自定义路线 - 包括当前URL

/{currentUrl}/{methodName} 

currentUrl不来作为参数。

例子:

/tool/compute/download 

,其中“下载”是方法的名称,和“/工具/计算/”是当前页面,我们都在。

只要提一下,我正在使用Sitecore。

任何人都可以帮忙吗?

谢谢。

+0

你能不能给我们一些样品,你实际上是靶向? – ramiramilu

+0

我刚刚编辑我的文章,并提供了一个例子 –

+0

'工具'是你的根目录名称,'compute'是控制器名称? – ramiramilu

回答

1

你可以建立,是以工具名称作为agument,像这样的方法:

[Route("tool/{toolName}/download")] 
    public HttpResponseMessage Get(string toolName) 
    { 
     var path = GetPathByToolName(toolName); 
     HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); 
     var stream = new FileStream(path, FileMode.Open); 
     result.Content = new StreamContent(stream); 
     result.Content.Headers.ContentType = 
      new MediaTypeHeaderValue("application/octet-stream"); 
     return result; 
    } 
+0

事情是{currentUrl}它是通过Sitecore生成的。我需要把它照原样。这甚至有可能吗? –

+0

我不知道sitecore,我不知道他们是否有解决方案。如果没有,我会尝试在运行时扩展路由。 http://www.strathweb.com/2014/07/building-strongly-typed-route-provider-asp-net-web-api/解释了如何扩展并将自己的自定义逻辑插入属性路由引擎。 – Oliver