簡體   English   中英

在 asp.net core 中將 index.html 設置為默認頁面

[英]Setting index.html as default page in asp.net core

如何從我的 wwwroot 中獲取 asp.net core 以提供 index.html 文件?

我想這樣做的原因是我使用 angular CLI 開發了一個 angular 4 應用程序,它負責整個構建過程。 我已將其設置為構建到我的 asp.net 核心項目的 wwwroot 目錄中,但 asp.net 核心不想為其提供服務。

起初我試圖通過控制器返回 html 文件。 我試過這條路線:

app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}");
    });

然后在控制器中,我像這樣返回 html 文件:

public IActionResult Index()
{
    var webRoot = _env.WebRootPath;
    var path = System.IO.Path.Combine(webRoot, "index.html");

    return File(path, "text/html");
}

這沒有用。 它返回了一個 404 not found 異常並給出了路徑,但它給出的路徑是 index.html 文件的正確路徑(我將它剪切並粘貼到資源管理器中並打開了文件)。

我也在啟動時聲明這些:

app.UseStaticFiles();
app.UseDefaultFiles();

然后我嘗試刪除默認路由。 現在我可以訪問 index.html 文件,但前提是我輸入文件名,即:

本地主機:58420/index.html

如果我嘗試在沒有指定“index.html”的情況下訪問域的根目錄,則會收到 404 錯誤。

將 index.html 引用為默認頁面的正確方法是什么? 我猜從控制器做這件事可能會更好,因為這樣它就會與角度路由兼容而無需重寫。

只需在startup.cs使用它:

app.UseFileServer();

它是以下的簡寫:

app.UseDefaultFiles();
app.UseStaticFiles();

避免了必須按正確順序排列的問題(如上所示)

我需要在 UseStaticFiles() 之前聲明 UseDefaultFiles()。

app.UseDefaultFiles();
app.UseStaticFiles();

安裝 NuGet 包Microsoft.AspNetCore.StaticFiles

現在,在Startup.Configure方法中,添加:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // Serve the files Default.htm, default.html, Index.htm, Index.html
    // by default (in that order), i.e., without having to explicitly qualify the URL.
    // For example, if your endpoint is http://localhost:3012/ and wwwroot directory
    // has Index.html, then Index.html will be served when someone hits
    // http://localhost:3012/
    //
    // (Function 1)
    app.UseDefaultFiles(); 




    // Enable static files to be served. This would allow html, images, etc. in wwwroot
    // directory to be served.
    //
    // (Function 2)
    app.UseStaticFiles();
}

注意:調用這些函數的順序很重要。 在 OO 編程中,很難不依賴於排序,因為對象維護的狀態在對象的生命周期內可能會發生變化。 (您猜對了,防止此類設計的一種解決方案是實現不變性。)

您現在應該從wwwroot目錄獲取文件(如果您想將其更改為其他內容,請使用UseWebRoot )。

來源: https : //docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files

app.UseDefaultFiles(new DefaultFilesOptions {
    DefaultFileNames = new List<string> { "index.html" }
});
app.UseStaticFiles();

這是最佳的,因為UseDefaultFiles URL 重寫器將只搜索index.html ,而不搜索遺留文件: default.htmdefault.htmlindex.htm

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2#serve-a-default-document

在startup.cs 里面的configureservices 方法中應用這個。 這里我們創建一個 DefaultFilesOption 對象,然后清除路徑中設置的所有默認文件。 接下來,我們添加要設置為默認文件的路徑。 然后我們使用' app.UseDefaultFiles(defaultfileoptions) 注入依賴項。 此外,我們需要注入靜態文件作為依賴項。

`

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
    DefaultFilesOption defaultFileOptions = new DefaultFilesOption();
    defaultFileOptions.DefaultFileNames.Clear();
    defaultFilesOptions.DefaultFileNames.Add("Index.html");
    app.UseDefaultFiles(defaultFileOptions);
    app.UseStaticFiles();
}  

`

如果要使用另一個文件(例如foo.html)作為默認文件,則可以在Startup.Configure方法中使用以下代碼來進行操作。

// Specify foo.html as the default document
DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
defaultFilesOptions.DefaultFileNames.Clear();
defaultFilesOptions.DefaultFileNames.Add("foo.html");
// Add Default Files Middleware
app.UseDefaultFiles(defaultFilesOptions);
// Add Static Files Middleware
app.UseStaticFiles();

方法二:

UseFileServer結合了UseStaticFilesUseDefaultFilesUseDirectoryBrowser中間件的功能。 DirectoryBrowser中間件可啟用目錄瀏覽,並允許用戶查看指定目錄中的文件。 我們可以將UseStaticFilesUseDefaultFiles中間件替換為UseFileServer中間件。

// Use UseFileServer instead of UseDefaultFiles & UseStaticFiles

FileServerOptions fileServerOptions = new FileServerOptions();
fileServerOptions.DefaultFilesOptions.DefaultFileNames.Clear();
fileServerOptions.DefaultFilesOptions.DefaultFileNames.Add("foo.html");
app.UseFileServer(fileServerOptions);

// 默認情況下,ASP.net core 不提供靜態文件,如 HTML、css、圖像等......我需要在請求處理管道中配置中間件來提供靜態文件。

// This code would be written in Startup.cs class for configuring the middleware components.



  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

        app.UseDefaultFiles(); // This sets the default page redirection for the in-comming request
            app.UseStaticFiles(); // This serves the static files to the client.

        }

您正在混合 MVC 和默認文件服務 (useDefaultFiles)。 從您的代碼中注釋掉以下幾行

app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}");
    });

並且只使用app.UseDefaultFiles(); . 它將開始工作。

return File(System.IO.File.OpenRead(Path.Combine(HostingEnvironment.WebRootPath + "/index.html")), "text/html");

它必須幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM