2015-06-30 31 views
1

我在自主託管的南希中提供靜態內容,並且已經重寫了根路徑,並且還在我的自定義引導程序中添加了靜態內容約定來重定向/內容,如下所示:NancyFX中的重定向/ to /index.html

conventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("/", "Content")); 

請求像http://localhost/index.html一個URL時,但我想http://localhost重定向到index.html的這工作得很好。雖然這似乎不起作用。我究竟做錯了什麼?

+0

也許只是從主模塊中的Get [「/」]重定向到index.html? – Corstiaan

+0

謝謝,我會試一試。我試圖避免僅僅爲此使用模塊;我希望它可以以某種方式配置! – Sam

+0

不是我所知道的,但我從來沒有真正瞭解到,所以我不知道 – Corstiaan

回答

0

我結束了一個簡單的模塊,像這樣解決這個:

public class IndexModule : NancyModule 
{ 
    public IndexModule() 
    { 
     Get["/"] = _ => Response.AsFile(Settings.Instance.GetFullContentPath("index.html")); 
    } 
} 

Settings.Instance.GetFullContentPath是我自己的方法它將磁盤上的完整路徑返回給指定的文件。

1

南希將發現並自動掛接任何IRequestStartup類。只要創建一個這樣的類,並且都將被罰款:)(超級騙子)

public class RootRedirector : IRequestStartup 
{ 
    public void Initialize(IPipelines pipelines, NancyContext context) 
    { 
     if (context.Request.Url.Path == "/") 
     { 
      throw new RouteExecutionEarlyExitException(new RedirectResponse("/index.html")); 
     } 
    } 
} 
+0

謝謝你。儘管如此,我不喜歡將異常用於非例外情況! :) – Sam