nphias
nphias

Reputation: 619

using servicestack to return xml files unchanged via REST

Hi I wanted to know if anyone has modified the restfiles example in servicestack, so that it can return unchanged xml files from the filesystem?

In other words, act as a webserver (IIS, cassini or apache etc.) but with REST mapping that comes with servicestack.

At the moment the response is preformated and i cant see how to change that.

Upvotes: 1

Views: 324

Answers (2)

nphias
nphias

Reputation: 619

I just want to show my solution to my original question in case others are looking to modify the restfiles example´s behaviour. the mod allows you to return xml or other filetypes to the browser without changing the content of the file:

public override object OnGet(Files request)
{
    var targetFile = GetAndValidateExistingPath(request);
    var isDirectory = Directory.Exists(targetFile.FullName);
    var hasParameters = this.RequestContext.AbsoluteUri.ToString().Contains("?") ? true : false;

    if (!isDirectory && request.ForDownload)
        return new HttpResult(targetFile, asAttachment:true);

    if (string.IsNullOrEmpty(targetFile.Name) || hasParameters)
    {
        var response = isDirectory
            ? new FilesResponse { Directory = GetFolderResult(targetFile.FullName) }
            : new FilesResponse { File = GetFileResult(targetFile) };
        return response;
    }
    return new HttpResult(targetFile);
}

So basically if the incomming request is empty or has parameters then we return a filesresponse otherwise we return an HttpResult directly displaying the files content. I hope this is useful to somebody.

Mythz: if there is something wrong in the code.. let us know. I tried to convert the example to .Net 4.0 but somewhere it breaks and the ajax file content viewing and listing part in /#!files doesnt show up. (perhaps i lost something in web.config during vs2010 conversion). Will you be releasing 4.0 compatible versions of the examples in the near future? ... Thanks again for the great work

Upvotes: 1

mythz
mythz

Reputation: 143319

The RestFiles example already shows how to return physical files from the filesystem:

return new HttpResult(targetFile, asAttachment:true);

This shows another example of using a custom HttpResult to return a downloaded file: https://stackoverflow.com/a/9843621/85785

Upvotes: 1

Related Questions