【发布时间】:2018-11-16 09:30:28
【问题描述】:
我有以下配置:
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。
我的 AngularController 和 Startup 类位于命名空间 MyNamespace
当我不使用 MVC 并访问 http://localhost:8000 时,它会在 WebApp 文件夹中加载 index.html 文件。现在对于所有其他请求(例如/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");
这些都不起作用。可能是我漏了一步?
【问题讨论】:
标签: c# angular asp.net-core asp.net-core-mvc