如何从Blazor WebAssembly中的目录读取文件

时间:2020-07-24 12:56:41

标签: c# file asp.net-core blazor webassembly

我目前正在开发.NET Standard 2.1 Blazor WebAssembly应用程序。我尝试从我的wwwroot目录中读取文件路径

Blazor WebAssembly应用程序未托管Asp.NET Core。

enter image description here

我尝试像这样从- name: get the file slurp: src: /etc/locale.gen register: slurped_file - name: initialize the matches list set_fact: MATCHES: [] - name: collect matches in a list set_fact: MATCHES: "{{ MATCHES + [line2match] }}" loop: "{{ file_lines }}" loop_control: loop_var: line2match vars: - decode_content: "{{ slurped_file.content | b64decode }}" - file_lines: "{{ decode_content.split('\n') }}" when: '"BE" in line2match' - name: report matches if any debug: msg: "Found {{ MATCHES | length }} matches\n{{ MATCHES }}" when: 'listlen | int > 0' vars: listlen: "{{ MATCHES | length }}" 的{​​{1}}方法中读取所有以.js结尾的文件路径:

Main

运行此命令时,我只会在浏览器中收到一条错误消息:Program.cs

enter image description here

您知道如何解决此问题吗?您知道如何从Blazor WASm(非ASP.NET Core托管)应用程序中的var filePaths = ReadWwwRootJSFilePaths(); private static string[] ReadWwwRootJSFilePaths() { Console.WriteLine("CurrentDirectory: " + Directory.GetCurrentDirectory()); var dir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "js"); Console.WriteLine("dir: " + dir); var files = Directory.GetFiles(dir); foreach (string file in files) { Console.WriteLine("file: " + file); } return files; } 目录中读取文件路径吗?

也许我还不太了解Blazor WebAssembly的概念(而不是托管ASP.NET Core)。实际上可以使用C#.NET Core功能吗?

1 个答案:

答案 0 :(得分:1)

是的,您只需对wwwroot中的文件执行HTTP调用即可。

要查看实际效果,请创建一个WebAssembly独立项目,其中FetchData调用位于wwwroot中的json文件。这是一些代码:

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

class A {
    public static Map<String, Class<? extends A>> REGISTRY = new HashMap<>();

    public String name = "test";
    public String id; // is B_ID when changed

    public A(String id) {
        this.id = id;
        this.getField("name") // should output test1
    }

    public Object getField(String key) throws NoSuchFieldException, SecurityException,
            IllegalArgumentException, IllegalAccessException {
        Field field = REGISTRY.get(this.id).getSuperclass().getDeclaredField(key);
        return field.get(this); // <- the issue, still returns "test"
    }
}


class B extends A {
    static {
        REGISTRY.put("B_ID", B.class);
    }

    public B() {
        super("B_ID");
        name = "test1";
    }
}

public class main {

    public static void main(String[] args) throws NoSuchFieldException, SecurityException,
            IllegalArgumentException, IllegalAccessException {
        new B(); // To update the registry
        A a = new A("B_ID");
        System.out.println(a.id);                //--> output: B_ID
        System.out.println(a.getField("name"));      //--> output: test, should be test1
    }

}