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

时间:2018-11-16 09:30:29

标签: c# angular asp.net-core asp.net-core-mvc

我有以下配置:

    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}");
        });
    }

我的Angular项目文件位于名称空间MyNamespace.WebApp中。

我的AngularControllerStartup类位于命名空间MyNamespace

当我不使用MVC并访问http://localhost:8000时,它会将index.html文件加载到WebApp文件夹中。现在,对于所有其他请求(例如/action),我已将其映射到AngularController,其外观如下:

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

我已经调试并验证了请求确实到达AngularController.Index()并返回了View("/index.html")。但是之后,我得到了500错误。我正在猜测,因为它找不到视图文件。

如何让MVC知道从嵌入式文件中提取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 个答案:

答案 0 :(得分:0)

在此示例中,我尝试在路径htmlpage.html中创建一个名称为Views/Home/htmlpage.html的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)))

在文件htmlpage.html中:

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

测试:

1


这种方式不需要:app.UseDefaultFiles();app.UseFileServer(...);

答案 1 :(得分:0)

我的解决方案是配置一个控制器,该控制器将使用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");
    }
}

使用依赖项注入来提供嵌入文件的AssemblyNamespace,以使其更加通用。您也可以将其硬编码到控制器中。