ServiceStack Plugin How to add MimeType for new file suffix, and allow the file suffix to be served? - servicestack

I would like to add the file suffix ".wasm" to the AllowFileExtensions property of the AppHost, and I'd like to associate the MimeType "application/wasm" to that file suffix, so that a Windows service based on ServiceStack can serve static files with this suffix. In my plugin's Configure method, I've tried this code, but it is not working.
public void Configure(IAppHost appHost) {
// Add the MIMEType application/wasm and associate it with .wasm files
MimeTypes.ExtensionMimeTypes["wasm"] = "application/wasm";
// Allow static files ending in .wasm to be served
var config = new HostConfig();
var allowFileExtensions = config.AllowFileExtensions;
allowFileExtensions.Add(".wasm");
}
Requests to my ServiceStack Windows service for static files ending in .wasm return a 403 error, and the Content-Type in the response headers is "text/plain".
Any suggestions on what I'm doing wrong, and how best to allow the new suffix and associate the new MimeType?

I've added wasm file extensions to ServiceStack's allowed File Extensions list in this commit. This change is available from v5.1.1 that's now available on MyGet.
For earlier versions of ServiceStack you can register an allowed File Extension by modifying IAppHost.Config, e.g:
public void Register(IAppHost appHost)
{
appHost.Config.AllowFileExtensions.Add("wasm");
}
You don't need to register a MimeType for wasm as the default MimeType for unknown Content-Types is application/{ext} which for .wasm returns application/wasm.

Related

Serving static files in ASP.NET 5 MVC 6

My wwwroot static files aren't being resolved.
I understand that to serve static files, I need to put them in wwwroot:
favicon.ico resolves just fine, but schema/v1-0.json does not. I get the generic message:
The resource you are looking for has been removed, had its name
changed, or is temporarily unavailable.
I have the following wired up in Startup:
app.UseMiddleware<StaticFileMiddleware>(new StaticFileOptions());
app.UseStaticFiles();
I am using DNX beta6. The above require beta5 packages. I cannot find anything online regarding serving static files in beta6. I am not sure if this could be the cause of the problem.
EDIT:
As per Sirwan's answer, I have added the following, but the json file is still not available:
var options = new StaticFileOptions
{
ContentTypeProvider = new JsonContentTypeProvider(),
ServeUnknownFileTypes = true,
DefaultContentType = "application/json"
};
app.UseStaticFiles(options);
The JsonContentTypeProvider class:
public class JsonContentTypeProvider : FileExtensionContentTypeProvider
{
public JsonContentTypeProvider()
{
Mappings.Add(".json", "application/json");
}
}
I can even see the file when browsing the server:
Try this:
app.UseStaticFiles(new StaticFileOptions
{
ServeUnknownFileTypes = true,
DefaultContentType = "image/x-icon"
});
If you have multiple file types that are unknown to ASP.NET you can use FileExtensionContentTypeProvider class:
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".json", "application/json");
provider.Mappings.Add(".ico", "image/x-icon");
// Serve static files.
app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider });
If you're using IIS, make sure you've added the correct mime-type mappings if you don't have a catch-all managed handler. Even though you don't need web.config for your website to work, IIS will still use that for your website.
Someone correct me if I'm wrong, but I believe that if you have not configured IIS to use a managed handler to serve static files it will still default to StaticFileModule and calling app.UseStaticFiles doesn't actually do anything. If you run it using dnx, however, then app.UseStaticFiles gets used.
Just a side note, you should probably also upgrade to beta7 if you haven't already.

ServiceStack: Serve static files with extension docx and zip

I have in the root of my web application two files: file1.docx and file2.zip neither of these files are served and instead I receive a 403 error. If I change the extension to .txt then the file gets served with no problem which leaves me to believe that SS looks at the extension?
.docx and .zip are in IISs list of known MIME types so I'm not sure why SS would serve the one and not the other as I thought the only check was that a physical file existed at that location.
Right, you need to add it to the Config.AllowFileExtensions safe whitelist, e.g:
SetConfig(new EndpointHostConfig {
AllowFileExtensions = { "docx", "zip" }
});

Can the ServiceStack config setting "WebHostPhysicalPath" be used for relative paths?

Hello ServiceStack aficionados!
I would like to host static XML files through the ServiceStack service; however, I can't seem to get the configuration right and only receive 404 errors. Feels like I tried all sorts of path/url combinations.
Can the WebHostPhysicalPath be defined as a relative path? Is there another setting that must be enabled? I was concerned that maybe the XML extension is conflicting with the format conversion stuff.
Also, can I host Razor cshtml files this way too?
Any comments on this approach?
thanks!
You can return a static file from a service like so:
[Route("/myFile/")]
public class GetMyFile
{
}
public class HelloService : Service
{
public HttpResult Any(GetMyFile request)
{
return new HttpResult(new FileInfo("~/myfile.xml"), asAttachment:true) { ContentType = "text/xml" };
}
}
As for razor: http://razor.servicestack.net/

Serving a static file with servicestack

How would i go around serving a static file using servicestack?
I would like to add a route like Routes.Add(/app) and when a client issues a GET for this path i need to return the a silverlight xap file.
ServiceStack is already be able to serve static files by referencing them directly.
Otherwise if you want a service return a file for downloading, you can do so with:
return new HttpResult(new FileInfo("~/app.xap"), asAttachment:true) {
ContentType = "application/x-silverlight-app"
};
Note: asAttachment will control whether or not to send HTTP Content-Disposition headers.
More info about ServiceStack's responses is in this earlier question: ServiceStack and returning a stream

Reading a file from a UNC path and setting the correct MIME type in a HTTP request

How would I go about reading a file from a UNC path, discovering the proper MIME type, and streaming that out to a browser?
It feels to me like I'm re-inventing IIS, and I'll also have to maintain my own MIME type database for each file extension. Does the above request sound reasonable, or is there a better way?
I plan on streaming this out via a browser HTTP Get request on IIS7. If it matters, I'm also running Cognos on the same server. Any framework is OK (WCF, ASPX, etc)
Using WCF its pretty basic:
This code can be hosted under IIS/Service/WAS/etc.
I never found a convenient way to handle the mime type, you will need to have your own db that will map file extension into mime types.
[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface IMediaRetriver
{
[OperationContract]
[WebGet(UriTemplate = "/get?f={fileName}")]
Stream Get(string fileName);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MediaRetriver : IMediaRetriver
{
public Stream Get(string fileName)
{
// pro tips
// this will cause the file dialog to show the file name instead of "get"
WebOperationContext.Current.OutgoingResponse.Headers.Add(
"Content-disposition", string.Format("inline; filename={0}", fileName));
WebOperationContext.Current.OutgoingResponse.ContentType =
"application/octet-stream";
// you want to add sharing here also
return File.Open(fileName)
}
}

Resources