2011-09-28 2 views
1

확장자가 .json 인 JSON이 포함 된 파일이 있습니다. 나는 다음과 같이 파일을 제공하는 낸시를 사용하고 있습니다 :Jason에서 사용하기 위해 Nancy와 함께 Json 파일 반환

Get["/schema/{file}"] = parameters => 
{ 
    return Response.AsFile(Directory.GetCurrentDirectory() + @"\sitefiles\schema\" + parameters.file as String); 
} 

을 나는이 같은 파일에 대한 jQuery를 아약스 요청을 만들 때 : 내가 파일을 다시 얻을

$.ajax({ 
      url: "/schema/auditResponseSchema.json", 
      type: 'GET', 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      cache: false, 
      success: function (responseSchema) { 
       console.log("get response data - Success"); 
       console.log(responseSchema); 
      }, 
      timeout: 5000, 
      error: function (xmlObj, textStatus, errorThrown) { 
       console.log("get response data - failed. status:" + textStatus + " error:" + errorThrown); 
      } 
     }); 

을하지만로 인식되지 않습니다 JSON, 반환되는 Content-Type이 application/octet-stream이기 때문에 이것이 확실합니다.

to the Content-Type을 application/json으로 변경하려면 어떻게해야합니까?

나는 시도했다 :

// this returns "application/json in the content. 
return Response.AsFile(Directory.GetCurrentDirectory() + @"\sitefiles\schema\" + parameters.file as String).Headers["Content-Type"] = "application/json"; 

// this returns the location of the file 
return Response.AsJson(Directory.GetCurrentDirectory() + @"\sitefiles\schema\" + parameters.file as String); 

내가 Response.AsJson를 문자열로 파일의 내용을 읽은 다음 사용하거나 헤더에게 Response.AsFile 반환을 변경할 수있는 방법이 있습니까?

답변

3

현재 확장 메서드를 사용하여 콘텐츠 형식을 재정의 할 수는 없지만 문제를 기록하고 0.8로 고정 시키겠다. 현재는 할 수 없습니다. 당신은, 그러나, 단지 직접 GenericFileResponse을 반환 할 수 있습니다

(모든 확장 메서드 인이 장면 뒤에 않습니다) :

return new GenericFileResponse(Directory.GetCurrentDirectory() + @"\sitefiles\schema\" + parameters.file as String, "application/json"); 
그래도 난 Directory.GetCurrentDirectory를 사용에 대해 권하고 싶습니다

, 당신은 할 수 있어야 그것을 상대 경로로 지정하십시오.

편집 : 당신이 관심이 https://github.com/NancyFx/Nancy/issues/315

편집이라면 여기에 문제를 기록 : 그리고 여기

+0

대단히 감사합니다 스티븐,이 지금 문제를 해결했습니다. 나는 0.8을 볼 것이다. – Dave

관련 문제