AngularJS templates with MVC

I am writing an AngularJS directive which uses templateURL

ie

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

      

I created a simple old .html file in the directory views/templates

, but every time I run this code MVC throws a null reference exception, like trying to find a controller for it (which doesn't exist). How do I stop MVC from trying to treat it like a file cshtml

, or how do I get it to treat it like a file cshtml

but doesn't require a controller? Seems wasteful to write a controller just for these little templates?

+3


source to share


2 answers


MVC generally prohibits the use of any content files in the Views folder (a special Views folder).

However, you can change this by tweaking your web.config.

Change this:

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

      

For this:



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

      

Example:

<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>

      

This way, only cshtml files are blocked from serving (html files are left alone).

+4


source


I think the easiest way is to add IgnoreRoute

in RouteConfig

.



routes.IgnoreRoute("views/templates/savingindicator.html");

      

0


source







All Articles