メインメニューを開く

差分

Nancy/静的コンテンツを扱う

1,607 バイト追加, 2014年11月4日 (火) 09:51
ページの作成:「Nancyでは初期状態でJavaScriptやCSSなどの静的コンテンツは除外されてしまい問答無用で404 Not Foundを返してくる。 どうしろと。 =...」
Nancyでは初期状態でJavaScriptやCSSなどの静的コンテンツは除外されてしまい問答無用で404 Not Foundを返してくる。
どうしろと。

==解決策(仮)==
DefaultNancyBootstrapperを継承したクラスをプロジェクトの直下においておくと勝手に読み込まれる模様。

そしてその中のConfigureConventionsメソッドを上書きして、以下のように静的コンテンツのあるディレクトリを書きまくれば解決した。
サブディレクトリも対象になるようだ。
本当にこれでいいのかは知らん。
<source lang="csharp">
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")
);
}
}
}
</source>

StaticContentConventionBuilder.AddDirectoryの書式は以下のような感じだと思う。
公開名がURLの一部となる部分で、公開ディレクトリがXamarin Studioのプロジェクト上のディレクトリになる。
「公開名」と「公開ディレクトリ」を同一にしておけば扱いやすいと思う。
本当にこれでいいのかは知らん。
<source lang="csharp">
StaticContentConventionBuilder.AddDirectory(公開名, 公開ディレクトリ)
</source>

{{stub}}
匿名利用者