-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