[英]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.