使用IIS Express在AngularJS项目中获取403错误

时间:2017-07-06 03:59:36

标签: c# angularjs asp.net-web-api

在AngularJS中,我正在尝试设置一个主页,其中列出了我的app / data / event文件夹中的事件(json文件)。控制器EventListController如下所示:

'use strict';

eventsApp.controller('EventListController', 
    function EventListController($scope, $location, eventData) {
        $scope.events = eventData.getAllEvents();
})

EventData服务如下所示:

eventsApp.factory('eventData', function ($resource) {
    var resource = $resource('/app/data/event/:id', { id: '@id' }, { "getAll": { method: "GET", isArray: true, params: { something: "foo" } } });
    return {
        getEvent: function (eventId) {
            return resource.get({
                id: eventId
            });
        },
        typeOf: function (obj) {
            return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();
        },
        getAllEvents: function () {
            return resource.query();
        },
        save: function (event) {
            return resource.save(event);
        }
    };

EventList.html如下所示:

<div>
    <h1>Events</h1>
    <hr/>
    <hr/>
    <ul class="thumbnails">
        <li ng-repeat="event in events|orderBy:sortorder" class="span5">
            <event-thumbnail/>
        </li>
    </ul>

</div>

我的路线看起来像这样:

'use strict';

var eventsApp = angular.module('eventsApp', ['ngResource', 'ngRoute'])
    .config(function ($routeProvider) {
        $routeProvider.when('/newEvent',
            {
                templateUrl: '/app/templates/NewEvent.html',
                controller: 'EditEventController'
            });
        $routeProvider.when('/eventDetails/:eventId',
           {
               templateUrl: '/app/templates/EventDetails.html',
               controller: 'EventController'
           });
        $routeProvider.when('/profile',
            {
                templateUrl: '/app/templates/EditProfile.html',
                controller: 'EditProfileController'
            });
        $routeProvider.when('/events',
            {
                templateUrl: '/app/templates/EventList.html',
                controller: 'EventListController'
            });
        $routeProvider.otherwise({ redirectTo: '/events' });
    });

WebAPI控制器(在单独的WebAPI项目中),EventController.cs,如下所示:

public class EventController : ApiController
    {
       public JToken Get(string id = null)
        {
            if (id==null)
            {
                return GetAllJsonEventsAsArray();
            }
            return GetSingleJsonFile(id);
        }
private JArray GetAllJsonEventsAsArray()
        {
            var path = System.Web.Hosting.HostingEnvironment.MapPath("/");
            var contents = "";
            foreach (var file in System.IO.Directory.GetFiles(path + "..app/data/event/"))
            {
                contents += System.IO.File.ReadAllText(file) + ",";
            }

            return JArray.Parse("[" + contents.Substring(0, contents.Length - 1) + "]");
        }

最后,WebApiConfig.cs看起来像这样:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

当我尝试访问http://localhost:49403/app/#/events时,我收到错误:GET http://localhost:49403/app/data/event/ 403(禁止)

任何人都可以帮忙吗?任何帮助都感激不尽。

1 个答案:

答案 0 :(得分:0)

默认情况下,IIS网站中的configuration/system.webServer/directoryBrowse@enabled设置为False。将其设置为True可以让我的应用程序浏览目录。