ASP.NET Core 1.0: 指定Static File中的文件作爲default page

指定一個網站的default page是很容易的事情。譬如IIS Management中,可以通過default page來指定,而默認的index.html, index.htm之類,則早已經被設置爲默認了。


另外一種對ASP.NET網站行之有效的方法是,修改web.config。

譬如:

 <system.webServer>
    <defaultDocument>
        <files>
          <clear />
          <add value="index.html" />
        </files>
      </defaultDocument>
</system.webServer>

但是,對於ASP.NET Core 1.0來說,它是Self Host的網站,所謂的deploy to IIS其實也只是安裝一個proxy。具體參閱ASP.NET Core最新的文章

https://docs.asp.net/en/latest/publishing/iis.html


那,如果需要設置static file (通常是wwwroot文件夾)的文件爲default page (這對純的JavaScript 網站來說非常有必要。static files的使用方法,同樣參閱ASP.NET Core的最新文檔:https://docs.asp.net/en/latest/fundamentals/static-files.html),

針對最新的ASP.NET Core 1.0 RC2,在Startup.cs中的Configure方法中加入下列代碼(替換index.html爲任意其他文件):

        public void Configure(IApplicationBuilder app)
        {
            app.UseStaticFiles();

            //var physicalFileSystem = new PhysicalFileSystem(webPath);
            var options = new FileServerOptions
            {
                EnableDefaultFiles = true,
                //StaticFileSystem = physicalFileSystem
            };
            //options.StaticFileOptions.FileSystem = physicalFileSystem;
            options.StaticFileOptions.ServeUnknownFileTypes = true;
            options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };
            app.UseFileServer(options);
        }


是爲之記。

Alva Chien
2016.5.25
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章