【发布时间】:2016-08-22 20:06:16
【问题描述】:
我正在使用 Aspnet Core rc1 构建 AngularJs 和 WebApi 应用程序。我在返回静态 index.html 文件时遇到问题。我尝试了几种方法。第一种方法是在Startup.cs中使用这样的代码
app.UseFileServer();
app.UseMvc();
如果我调用http://localhost:29838(root url),它会以这种方式工作。但是如果我继续http://localhost:29838/books(/books root 是我使用 ng-route 定义的角度根,并且我使用的是 html5 模式)并更新页面,服务器当然会返回 404 错误。
然后,我阅读了这篇文章https://dzone.com/articles/fixing-html5mode-and-angular。我尝试在web.config/ 中使用重写模块方法,一切正常。但我不喜欢这种方法。据我了解,它仅适用于 IIS。
最后,我尝试使用第一种方式,在文章中描述(当Home/Index返回html文件时)。控制器代码为:
public ActionResult Index()
{
return File("~/index.html", "text/html");
}
而Startup.cs 是:
routes.MapRoute(
name: "Default",
url: "{*.}",
defaults: new
{
controller = "Home",
action = "Index",
}
);
使用这种方法我有这样的错误:Refused to load the script 'http://localhost:29838/libs/jquery/dist/jquery.min.js' because it violates the following Content Security Policy directive: "script-src 'unsafe-inline'". 我无法克服这一点。我做错了什么?
【问题讨论】:
-
你试过用
app.UseStaticFiles();/和app.UseDefaultFiles(); -
是的。我努力了。事实上,当我从控制器返回 html 时,我无法理解脚本起源问题。我看过很多关于这个的教程
-
凹凸。我不知道...我将 angular2 与 VS2015 和 MVC5 一起使用,有很多问题,但没有像您这样的问题...而且我认为您不应该使用 JQuery,因为 Angular 中已经有 JQLite。
标签: angularjs html asp.net-web-api asp.net-core