[英]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. 因此,我正在用Nancy创建一个API,以允许index.html在GET请求中包含CSS和javascript文件。
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. 我也尝试在引导程序中添加静态目录,但出现相同的“ ERR_CONNECTION_RESET”错误。
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): 编辑:因此,我添加了以下代码行,并且mime类型已修复,将进行更多调整(可能是更好的方法):
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. 做到了把戏,我会用这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.