![](/img/trans.png)
[英]Nancy self hosting HttpListenerException: “The parameter is incorrect”
[英].NET Nancy response with a video file (self hosting)
我正在使用Nancy(版本1.4.4)基於REST體系結構在.NET上編寫自托管服務器。 我更喜歡進行自我托管(Nancy.Hosting.Self為1.4.1版),其中一項要求功能是使用視頻文件響應請求。 為了使畫面清晰,我的伙伴編寫了React應用程序,他需要此視頻。
我嘗試了不同的選擇:
首先,我嘗試使用Response.AsFile()
但是當我嘗試通過鏈接訪問它時,我得到404標簽為“找不到您所請求的資源”。 我不知道為什么
public class HelloModule : NancyModule
{
public HelloModule()
{
Get["/"] = parameters =>
{
return Response.AsFile(@"C:\7\video112018.mp4","video/mp4");
};
}
}
第二個變種是像下面的代碼一樣使用GenericFileResponce
,但這會導致相同的問題:
public class HelloModule : NancyModule
{
public HelloModule()
{
Get["/"] = parameters =>
{
GenericFileResponse fileResponse = new GenericFileResponse(@"C:\7\video112018.mp4");
return fileResponse;
};
}
}
我嘗試的最后一個選擇是直接寫入響應流,如下面的代碼所示,但是在這種情況下會出現錯誤“指定的網絡名稱不再可用”。 棘手的是,有時會發生此錯誤,但我沒有發現它來自哪里的任何依賴關系...
public class HelloModule : NancyModule
{
public HelloModule()
{
Get["/"] = parameters =>
{
return new Response
{
ContentType = "video/mp4",
Contents = s =>
{
String fileName = @"C:\7\video112018.mp4";
using (var stream = new FileStream(fileName, FileMode.Open))
stream.CopyTo(s);
s.Flush();
s.Close();
}
};
};
}
}
如果您對這些解決方案有任何建議或提出其他建議,我將不勝感激。
PS我也嘗試發送圖像,它可以使用第三種方法,但不能使用第一種或第二種方法
PPS並不嚴格地判斷代碼,因為這只是示例)
通過執行以下操作,我能夠進行非常基本的視頻文件流傳輸:
Get["/"] = p =>
{
var file = new FileStream(@"PATH_TO_FILE", FileMode.Open);
return new StreamResponse(() => file, "video/mp4");
}
這樣可以播放視頻文件,但是沒有進行搜索。
最后我找到了這個帖子。 添加這些擴展名可以搜索視頻。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.