ASP.NET API Controller method custom route - Include current URL

143 views Asked by At

I am trying to create a custom route for an API Controller, that has the following structure:

/{currentUrl}/{methodName}

currentUrl does not come as a parameter.

Example:

/tool/compute/download

where "download" is the name of the method, and "/tool/compute/" is the current page we are on.

Just to mention, I am using Sitecore.

Could anyone help?

Thanks.

1

There are 1 answers

2
Oliver On

You could build a method that takes the tool name as an agument, like this:

    [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;
    }