dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

use Microsoft.Extensions.FileProviders.Embedded ,when file name is xx.cs.xx ResourcePath is wrong #24314

Closed luoyunchong closed 4 years ago

luoyunchong commented 4 years ago

Describe the bug

I use Microsoft.Extensions.FileProviders.Embedded

When the embedded file name is in the form of xxxx.cs.xxxx.

ResourcePath value cannot be recognized

image

code is simple,

using Microsoft.Extensions.FileProviders;
using System;
using System.IO;
using System.Reflection;
using System.Text;

namespace FileTest
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] paths = {
                "ABC.html.txt",
                "ABC.cs.txt"
            };

            foreach (var path in paths)
            {
               Embedded("./" + path);
            }

            Console.WriteLine("over!");
        }

        static void Embedded(string path)
        {
            IFileProvider fileProvider = new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly());
            IFileInfo fileInfo = fileProvider.GetFileInfo(path);
            string text;
            using (Stream stream = fileInfo.CreateReadStream())
            {
                using var streamReader = new StreamReader(stream, Encoding.UTF8, true);
                text = streamReader.ReadToEnd();
            }
            Console.WriteLine(text);
        }
    }
}

To Reproduce

FileTest.zip

Exceptions (if any)

at Microsoft.Extensions.FileProviders.Embedded.Manifest.ManifestFileInfo.CreateReadStream() at FileTest.Program.Embedded(String path) in C:\Users\Computer\Desktop\FileTest\Program.cs:line 32 at FileTest.Program.Main(String[] args) in C:\Users\Computer\Desktop\FileTest\Program.cs:line 21

Further technical details

Host (useful for support): Version: 5.0.0-preview.6.20305.6 Commit: 4ba9ecaabd

.NET SDKs installed: 3.1.302 [C:\Program Files\dotnet\sdk] 3.1.400-preview-015178 [C:\Program Files\dotnet\sdk] 5.0.100-preview.6.20318.15 [C:\Program Files\dotnet\sdk]


-  Microsoft Visual Studio Enterprise 2019 Preview
版本 16.7.0 Preview 3.1
mkArtakMSFT commented 4 years ago

@javiercn any recommendation where this should go? (mvc or platform) ?

javiercn commented 4 years ago

Use ManifestFileEmbeddedProvider which is designed to be full-fidelity or instead of that, use static web assets if possible. This is a known limitation of EmbeddedFileProvider we don't plan to address.

javiercn commented 4 years ago

Also, not sure if this moved to the runtime repo.

Update: It didn't, but we don't plan to address this.

luoyunchong commented 4 years ago

Thanks,I use Microsoft.Extensions.FileProviders . physical can also solve my problem