[英]Nancy response not returning proper MIME Type for index.html including
因此,我正在用Nancy创建一个API,以允许index.html在GET请求中包含CSS和javascript文件。
我目前遇到的问题是:
我用于实现此目的的南希代码是:
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;
};
当我调试时,内容类型是正确的,但是不能正确解释。 我已经看到,返回内容而不是完整响应可能是一个问题,但是当我这样做时,出现此错误:
GET http://localhost:8090/js/controllers.js net::ERR_CONNECTION_RESET
我也尝试在引导程序中添加静态目录,但出现相同的“ ERR_CONNECTION_RESET”错误。
为了进行合理性测试,即使我不正确地直接调用它也可以获得正确的文件,例如:
任何帮助表示赞赏!
编辑:因此,我添加了以下代码行,并且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;
做到了把戏,我会用这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.