Search code examples
sitefinity

sitefinity ControllerToolboxItem vs MVC Controller


I have built a MVC Controller that will be used throughout the site to download files for an API. I am not able to call it like normal with the Mvc\controllers\ControllerName\method. I get a 404 error page not found. I did do so testing with adding ControllerToolboxItem and adding it to a page. Once I add it to the page I can call the method. Using the PageName\method?filePath="" so I know the method is working. Is there something I am missing in my Controller that I need to add for it to be called throughout the site?

public class CommonController : Controller
    {
        [HttpGet]
    public HttpResponseMessage GetDataFileResponse(string filePath) {
        try {
    
            FileStream fileStream = File.OpenRead(filePath);
            long fileLength = new FileInfo(filePath).Length;
    
            var response = new HttpResponseMessage();
            response.Content = new StreamContent(fileStream);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = "mydata.csv";
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentLength = fileLength;
            return response;
        } catch (Exception e) {
            Console.WriteLine(e);
            throw;
        }
    }
}

Solution

  • You can add the widget to a base page template, so now it will be available on all pages.

    Or see if you can make it an web api controller and then there will be no widget, but you will be able to access it via /yourapiroute/common/getdataFileResponse?filepath=xxx

    I've written an article in the past, which might be of help: https://sitefinitydevelopment.com/blog/uploading-files-to-sitefinity-asynchronously-using-kendo-ui-upload.html