我正在编写一个使用 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/