asp.net-mvc - 带有 MVC 的 AngularJS 模板

标签 asp.net-mvc angularjs razor

我正在编写一个使用 templateURL 的 AngularJS 指令,即

m.directive('mySavingIndicator', function () {
    return {
        restrict: 'E',
        templateUrl: '/views/templates/savingindicator.html'
    };
});

我在 views/templates 目录中创建了一个普通的旧 .html 文件,但每次运行此代码时,MVC 都会抛出一个空引用异常,就像它试图为其查找 Controller 一样(该 Controller 不存在) )。如何阻止 MVC 尝试将其视为 cshtml 文件,或者如何使其将其视为 cshtml 文件但不需要 Controller ?仅仅为这些小模板编写 Controller 似乎很浪费?

最佳答案

MVC 通常会阻止从 Views 文件夹提供任何内容文件(Views 文件夹很特殊)。

但是,您可以通过调整 web.config 来更改此设置。

更改此:

<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode"    type="System.Web.HttpNotFoundHandler" />

对此:

<add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />

示例:

<handlers>
  <add name="JavaScriptHandler" path="*.js" verb="*"
     preCondition="integratedMode" type="System.Web.StaticFileHandler" />      
  <add name="HtmlScriptHandler" path="*.html" verb="*"
     preCondition="integratedMode" type="System.Web.StaticFileHandler" />
  <remove name="BlockViewHandler"/>
  <add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>

这样,只有 cshtml 文件会被阻止直接提供服务(html 文件将被保留)。

关于asp.net-mvc - 带有 MVC 的 AngularJS 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27376401/

相关文章:

sql - 参数异常 : Keyword not supported: 'server'

javascript - AngularJS 中 ng-include 内的范围丢失

AngularJS : Listen for click events on a button outside of a directive

c# - 更改 RazorViewEngine 以在特定项目 (.Net Core) 中查找 View

asp.net - 如果我使用 InRequestScope(),在哪里执行 DBContext.SaveChanges()

javascript - .Net MVC 如何将脚本添加到各个 View

asp.net-mvc - 在我的 MVC4 站点中的排序或分页操作期间,如何让我的 WebGrid 以 POST 而不是 GET?

javascript - AngularJS + Fullcalendar 发送错误TypeError : Cannot read property '__id' of undefined

c# - 在 Html.DropDownlistFor 中获取多个选定值

vb.net - VB.Net 中的 CheckBoxFor Nullable Boolean