NancyFx查询JSON文件以从请求URL中剥离JSON扩展名 [英] NancyFx Queries to JSON files stripping the JSON extension from request URL
问题描述
在一个项目中,我正在使用Nancy通过Nancy Self-Host提供基本的Web内容.这通常是可行的,但不幸的是,在端点(即http://localhost/data.json
)上运行查询会导致模块接收到http://localhost/data
的请求URL.
In one project I'm using Nancy to serve basic web content via Nancy Self-Host. This generally works, but unfortunately, running queries on endpoints ie http://localhost/data.json
results in the module receiving a request url of http://localhost/data
.
当我查询localhost/data.json
时,我会在JSON中收到nancy生成的404响应....我不知道为什么会这样,并且找不到任何地方记录的这种行为.
When I query localhost/data.json
I get a nancy-generated 404 response... in JSON. I have no clue why this is happening, and can't find this behavior documented anywhere.
这是我的模块:
public class NancySimpleWebModule : NancyModule
{
/// <summary>
/// TODO - HACK!
/// </summary>
public static NancySimpleWebServer WebServer;
public NancySimpleWebModule()
{
Get["/"] = Get[@"/{url*}"] = _ =>
{
string filePath = WebServer.ResolveFilePath(Request.Url.Path.Trim('/', '\\'));
if (filePath == null || filePath.Length == 0 || !File.Exists(filePath))
return new Response { StatusCode = HttpStatusCode.NotFound };
return File.ReadAllText(filePath);
};
}
}
这是我启动服务器的方式:
Here's how I start the server:
_host = new NancyHost(
new HostConfiguration { UrlReservations = new UrlReservations { CreateAutomatically = true } },
uriToBind);
_host.Start();
任何想法或建议都将不胜感激.
Any thoughts or suggestions would be greatly appreciated.
推荐答案
According to #1919, #2671 and #2711 this is by design and you're unable to disable it:
这是内容协商的功能
This is a feature of content negotiation.
.xml
和.json
都会发生这种情况.
This happens for both .xml
and .json
.
建议的解决方法是在扩展名(GET /foo/bar.json/baz
)之后添加内容或重命名文件(/foo/bar.js
).
Suggested workarounds would be to add something after the extension (GET /foo/bar.json/baz
) or to rename the files (/foo/bar.js
).
这篇关于NancyFx查询JSON文件以从请求URL中剥离JSON扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!