代码之家  ›  专栏  ›  技术社区  ›  Xpleria Dholu

如何从Asp.Net核心控制器返回嵌入的静态html文件?

  •  0
  • Xpleria Dholu  · 技术社区  · 7 年前

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseDefaultFiles();
    
            app.UseFileServer(new FileServerOptions
            {
                FileProvider = new EmbeddedFileProvider(typeof(Startup).Assembly, typeof(Startup).Namespace + ".WebApp"),
                RequestPath = string.Empty
            });
    
            app.UseCors("CorsPolicy");
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "angular",
                    defaults: new {controller = "Angular", action = "Index"},
                    template: "{*url}");
            });
        }
    

    我的角度项目文件在命名空间中 MyNamespace.WebApp

    我的 AngularController Startup MyNamespace

    http://localhost:8000 它装载 index.html 文件在 WebApp 文件夹。对于所有其他请求(例如 /action )我已经把它映射到 角度控制器 ,如下所示:

    public class AngularController : Controller
    {
        public IActionResult Index() {
            return View("/index.html");
        }
    }
    

    我已经调试并验证了请求 AngularController.Index() 和回报 View("/index.html") . 但在那之后我得到了500个错误。我猜是因为它找不到视图文件。

    index.html索引

    我试过以下方法:

    return View("~/index.html");
    return View("index.html");
    return View("~/../index.html");
    return View("../index.html");
    return View("~/WebApp/index.html");
    return View("WebApp/index.html");
    

    这些都不管用。可能我漏了一步?

    2 回复  |  直到 7 年前
        1
  •  1
  •   Tân    7 年前

    htmlpage.html 在路上 Views/Home/htmlpage.html .

    在文件中 Index.cshtml (路径: Views/Home/Index.cshtml ):

    @using Microsoft.AspNetCore.Hosting
    @using System.IO
    @inject IHostingEnvironment environment
    @{ 
        string htmlPath = "Views/Home/htmlpage.html";
    }
    
    @Html.Raw(File.ReadAllText(System.IO.Path.Combine(environment.ContentRootPath, htmlPath)))
    

    在文件中 html页面.html :

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>bar</title>
    </head>
    <body>
        <h3>foo</h3>
    </body>
    </html>
    

    1


    这样不需要: app.UseDefaultFiles(); app.UseFileServer(...);

        2
  •  0
  •   Xpleria Dholu    7 年前

    EmbeddedFileProvider 并获取嵌入文件的内容,如下所示:

    public AngularController: Controller
    {
        private IAppSettings appSettings;
    
        public AngularController(IAppSettings appSettings)
        {
            this.appSettings = appSettings;
        }
    
        public IActionResult Index()
        {
            var fileProvider = new EmbeddedFileProvider(this.appSettings.WebAssembly, this.appSettings.WebNamespace);
            var contents = this.fileProvider.GetDirectoryContents(string.Empty);
    
            IFileInfo index = null;
            foreach (var file in contents)
            {
                if (file.Name.Equals("index.html"))
                {
                    index = file;
                    break;
                }
            }
    
            if (index == null)
            {
                throw new Exception("'index.html' not found");
            }
    
            var reader = new StreamReader(index.CreateReadStream());
            var text = reader.ReadToEnd();
            return this.Content(text, "text/html");
        }
    }
    

    这个 Assembly Namespace 其中的嵌入文件是使用依赖注入提供的,以使其更通用。你也可以硬编码到控制器中。

    推荐文章