简体   繁体   中英

Nancy response not returning proper MIME Type for index.html including

So I'm creating an API with Nancy to allow my index.html to include css and javascript files with a GET request.

The problem I'm currently getting is this:

在此处输入图片说明

The Nancy code i'm using to achieve this is:

        Get["/{path*}"] = api => {
            var parentDir = Path.GetDirectoryName(Application.StartupPath);
            var path = String.Concat(parentDir, "\\Ui\\", api.path);
            if(!File.Exists(path)) {
                return new TextResponse(HttpStatusCode.NotFound);
            }
            return Response.AsFile((String)api.path, (String)MimeMapping.GetMimeMapping(api.path)).Contents;
        };

The content-type is correct when I debug but it isn't interpreting it properly. I've seen that it might be an issue with me returning the contents instead of the full response but when I do that I get this error:

GET http://localhost:8090/js/controllers.js net::ERR_CONNECTION_RESET

I've also tried adding static directories in the bootstrapper but I get the same "ERR_CONNECTION_RESET" error.

For a test of sanity, I do get the proper file if I directly call it even if it isn't proper, for example:

在此处输入图片说明

Any help is appreciated!

EDIT: So I added these lines of code and the mime type was fixed, going to do some more tweaking (might be a better way):

        var fileContents = Response.AsFile((String)api.path).Contents;
        var response = new Response();
        response.ContentType = (String)MimeMapping.GetMimeMapping(api.path);
        response.Contents = fileContents;
        return response;
    var fileContents = Response.AsFile((String)api.path).Contents;
    var response = new Response();
    response.ContentType = (String)MimeMapping.GetMimeMapping(api.path);
    response.Contents = fileContents;
    return response;

Did the trick, I will use this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM