婷婷久久综合九色综合,欧美成色婷婷在线观看视频,偷窥视频一区,欧美日本一道道一区二区

<tt id="bu9ss"></tt>
  • <span id="bu9ss"></span>
  • <pre id="bu9ss"><tt id="bu9ss"></tt></pre>
    <label id="bu9ss"></label>

    當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

    asp .net core靜態(tài)文件資源的深入講解

     2020-11-11 11:25  來源: 腳本之家   我來投稿 撤稿糾錯(cuò)

      阿里云優(yōu)惠券 先領(lǐng)券再下單

    這篇文章主要給大家介紹了關(guān)于asp .net core靜態(tài)文件資源的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

    前言

    對(duì)靜態(tài)資源的簡單的一個(gè)概況,在《重新整理.net core 計(jì)1400篇》系列后面會(huì)深入。

    正文

    我們?cè)诩尤胫虚g件是這樣寫的:

    app.UseStaticFiles();

    默認(rèn)是給wwwroot提供資源。

    那么我訪問https://localhost:44330/js/site.js 資源,就可以訪問到。

    // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification

    // for details on configuring this project to bundle and minify static web assets.

    // Write your JavaScript code.

    同樣我們可以自定義路徑。

    app.UseStaticFiles(new StaticFileOptions {

    FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")),

    RequestPath="/static"

    });

    上面在根目錄下的static建立路由,路由路徑static 為標(biāo)識(shí)。

    訪問:

    https://a5img.pncdn.cn//

    就能看到一張圖片了。

    同樣再次訪問,https://localhost:44330/js/site.js 依然可以訪問,看了這個(gè)wwwroot 是一個(gè)釘子戶,無論如何添加還是存在的。

    const string cacheMaxAge = "60480";
    app.UseHttpsRedirection();
    app.UseStaticFiles(new StaticFileOptions {
     FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")),
     RequestPath="/static",
     OnPrepareResponse = ctx => {
      ctx.Context.Response.Headers.Append("cache-control", $"public,max-age={cacheMaxAge}");
     }
    }
    );

    可以設(shè)置一些緩存。

    靜態(tài)文件授權(quán)

    官方倒是提供了兩種方法。

    一種是,讓靜態(tài)文件路由放到權(quán)限之后。

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseStaticFiles(new StaticFileOptions
    {
    FileProvider = new PhysicalFileProvider(
    Path.Combine(env.ContentRootPath, "Static")),
    RequestPath = "/static"
    });

    另一種比較自定義高:

    [Authorize]
    public IActionResult BannerImage()
    {
    var filePath = Path.Combine(
    _env.ContentRootPath, "MyStaticFiles", "images", "red-rose.jpg");

    return PhysicalFile(filePath, "image/jpeg");
    }

    可以根據(jù)參數(shù)做一些邏輯變化。

    但是這些方式比較影響性能,一般來說靜態(tài)文件是開放的,而用戶上傳的文件是通過加密的,放在存儲(chǔ)服務(wù)器上。

    當(dāng)然小型項(xiàng)目,可以用用。

    靜態(tài)文件目錄

    Configure中添加:

    app.UseDirectoryBrowser(new DirectoryBrowserOptions
    {
    FileProvider=new PhysicalFileProvider(Path.Combine(env.ContentRootPath,"Static")),
    RequestPath="/static"
    });

    這個(gè)中間件注入的位置是應(yīng)該在UseRouting之前的,同樣是性能問題。

    然后在ConfigureServices中添加:

    services.AddDirectoryBrowser();

    效果:

    這種方式呢,一般只是在dev環(huán)境下打開,真正的生產(chǎn)環(huán)境由于安全問題就不打開的。

    默認(rèn)文檔

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

    app.UseStaticFiles(); 才是真正的路由。

    app.UseDefaultFiles(); 只是說提供一些參數(shù),比如配置下面這些為默認(rèn)項(xiàng)。

    default.htm
    default.html
    index.htm
    index.html

    其實(shí)是這樣一個(gè)過程,app.UseStaticFiles() 如果沒有找到相應(yīng)的路由,那么應(yīng)該給下一個(gè)中間件。

    如果調(diào)用了app.UseDefaultFiles(),那么會(huì)去找是否存在默認(rèn)項(xiàng),默認(rèn)是去wwwroot 下尋找上述的默認(rèn)項(xiàng)。

    默認(rèn)文檔可以進(jìn)行修改:

    var options = new DefaultFilesOptions();
    options.DefaultFileNames.Clear();
    options.DefaultFileNames.Add("mydefault.html");
    app.UseDefaultFiles(options);
    app.UseStaticFiles();

    UseFileServer 結(jié)合了 UseStaticFiles、UseDefaultFiles 和 UseDirectoryBrowser(可選)的功能。

    app.UseFileServer(enableDirectoryBrowsing: true);

    enableDirectoryBrowsing 表示是否使用UseDirectoryBrowser。

    FileExtensionContentTypeProvider

    FileExtensionContentTypeProvider 類包含 Mappings 屬性,用作文件擴(kuò)展名到 MIME 內(nèi)容類型的映射。

    比如說我去訪問:https://localhost:44330/static/test.myapp

    我在static 下有test.mapp 這個(gè)文件,但是靜態(tài)文件處理并沒有去處理。

    原因:

    客服端發(fā)了這樣一個(gè)請(qǐng)求,人家接受這些流,但是服務(wù)器找到到,myapp 對(duì)應(yīng)的媒體類型,那么這個(gè)時(shí)候客戶端就不會(huì)接受了,服務(wù)端也認(rèn)為沒有找到。

    官方給了例子:

    var provider = new FileExtensionContentTypeProvider();
    // Add new mappings
    provider.Mappings[".myapp"] = "application/x-msdownload";
    provider.Mappings[".htm3"] = "text/html";
    provider.Mappings[".image"] = "image/png";
    // Replace an existing mapping
    provider.Mappings[".rtf"] = "application/x-msdownload";
    // Remove MP4 videos.
    provider.Mappings.Remove(".mp4");

    app.UseDefaultFiles();
    app.UseStaticFiles();
    app.UseStaticFiles(new StaticFileOptions
    {
    FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")),
    RequestPath = "/static",
    OnPrepareResponse = ctx => {
    ctx.Context.Response.Headers.Append("cache-control", $"public,max-age={cacheMaxAge}");
    },
    ContentTypeProvider= provider
    }

    給他加一個(gè)媒體類型,認(rèn)為myapp 應(yīng)該是一個(gè)需要下載文件。

    然后運(yùn)行之,然后就會(huì)出現(xiàn)下載。

    同樣,我們寫的是.html,如果我們不喜歡可以去寫.htm3也行。

    https://localhost:44330/static/index.htm3

    結(jié)果:

    因?yàn)閜rovider.Mappings[".htm3"] = "text/html"; ,.htm3被映射成了text/html,那么客戶端就按照這種格式處理。所以模板引擎就可以多樣性,有興趣自己也可以去設(shè)計(jì)。

    這就是媒體類型映射。

    如果是媒體類型未知的情況下,那么可以這樣:

    app.UseStaticFiles(new StaticFileOptions
    {
    FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Static")),
    RequestPath = "/static",
    OnPrepareResponse = ctx => {
    ctx.Context.Response.Headers.Append("cache-control", $"public,max-age={cacheMaxAge}");
    },
    ServeUnknownFileTypes = true,
    DefaultContentType = "image/png"
    }
    );

    ServeUnknownFileTypes true

    DefaultContentType "image/png" 讓客戶端按照?qǐng)D片處理。

    但是官方給了建議。

    啟用 ServeUnknownFileTypes 會(huì)形成安全隱患。 它默認(rèn)處于禁用狀態(tài),不建議使用。

    FileExtensionContentTypeProvider 提供了更安全的替代方法來提供含非標(biāo)準(zhǔn)擴(kuò)展名的文件。

    總結(jié)

    到此這篇關(guān)于asp .net core靜態(tài)文件資源的文章就介紹到這了,更多相關(guān)asp .net core靜態(tài)文件資源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

    來源:腳本之家

    鏈接:https://www.jb51.net/article/194472.htm

    申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

    相關(guān)標(biāo)簽
    asp.net
    資源

    相關(guān)文章

    熱門排行

    信息推薦