【发布时间】:2015-02-24 04:49:51
【问题描述】:
所以我正在使用 Nancy 创建一个 API,以允许我的 index.html 包含带有 GET 请求的 css 和 javascript 文件。
我目前遇到的问题是这样的:
我用来实现此目的的 Nancy 代码是:
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;
【问题讨论】:
标签: javascript c# html nancy