「Nancy/静的コンテンツを扱う」の版間の差分
ページの作成:「Nancyでは初期状態でJavaScriptやCSSなどの静的コンテンツは除外されてしまい問答無用で404 Not Foundを返してくる。 どうしろと。 =...」 |
|||
| 21行目: | 21行目: | ||
base.ConfigureConventions(conventions); | base.ConfigureConventions(conventions); | ||
// ここに追記する | |||
// サブディレクトリも勝手に追加されている模様 | |||
conventions.StaticContentsConventions.Add( | conventions.StaticContentsConventions.Add( | ||
StaticContentConventionBuilder.AddDirectory("Scripts", @"Scripts") | StaticContentConventionBuilder.AddDirectory("Scripts", @"Scripts") | ||
); | |||
conventions.StaticContentsConventions.Add( | |||
StaticContentConventionBuilder.AddDirectory("Content", @"Content") | |||
); | ); | ||
} | } | ||
2014年11月4日 (火) 09:53時点における版
Nancyでは初期状態でJavaScriptやCSSなどの静的コンテンツは除外されてしまい問答無用で404 Not Foundを返してくる。 どうしろと。
解決策(仮)
DefaultNancyBootstrapperを継承したクラスをプロジェクトの直下においておくと勝手に読み込まれる模様。
そしてその中のConfigureConventionsメソッドを上書きして、以下のように静的コンテンツのあるディレクトリを書きまくれば解決した。 サブディレクトリも対象になるようだ。 本当にこれでいいのかは知らん。
using System;
using Nancy;
using Nancy.Conventions;
namespace Test
{
public class AppBootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureConventions(NancyConventions conventions)
{
base.ConfigureConventions(conventions);
// ここに追記する
// サブディレクトリも勝手に追加されている模様
conventions.StaticContentsConventions.Add(
StaticContentConventionBuilder.AddDirectory("Scripts", @"Scripts")
);
conventions.StaticContentsConventions.Add(
StaticContentConventionBuilder.AddDirectory("Content", @"Content")
);
}
}
}
StaticContentConventionBuilder.AddDirectoryの書式は以下のような感じだと思う。 公開名がURLの一部となる部分で、公開ディレクトリがXamarin Studioのプロジェクト上のディレクトリになる。 「公開名」と「公開ディレクトリ」を同一にしておけば扱いやすいと思う。 本当にこれでいいのかは知らん。
StaticContentConventionBuilder.AddDirectory(公開名, 公開ディレクトリ)