将站点托管到 IIS 后,AngularJS 路由不起作用 [英] AngularJS routing not working after site hosted into IIS

查看:31
本文介绍了将站点托管到 IIS 后,AngularJS 路由不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 ASP.NET MVC 框架中使用 AngularJS 创建 SPA 技术,当它从 VS2013 运行时,页面之间的 AngularJS 路由工作正常,但在将应用程序托管到 IIS7.5 后路由不起作用,

We are creating SPA technique using AngularJS in ASP.NET MVC framework,AngularJS routing between pages are working fine when it's run from VS2013,but after hosting the application in to IIS7.5 routing not working,

路由脚本:

   var appRoot = angular.module('main', ['ngRoute', 'ngGrid', 'ngResource']);     //Define the main module

    appRoot
        .config(['$routeProvider', function ($routeProvider) {
            //Setup routes to load partial templates from server. TemplateUrl is the location for the server view (Razor .cshtml view)
            $routeProvider
                .when('/home', { templateUrl: '/home/main', controller: 'HomeController' }) .otherwise({ redirectTo: '/home' });
        }])
        .controller('RootController', ['$scope', '$route', '$routeParams', '$location', function ($scope, $route, $routeParams, $location) {
            $scope.$on('$routeChangeSuccess', function (e, current, previous) {
                $scope.activeViewPath = $location.path();
            });
        }]);

索引.html:

 <li class="mt" data-ng-class="{active : activeViewPath==='/home'}">
                    <a href='#/home'>
                        <i class="fa fa-dashboard"></i>
                        <span>Dashboard Home</span>
                    </a>
                </li>


  <div class="col-lg-9 main-chart" ng-view="">
                </div>

项目结构:

推荐答案

你在 IIS 上运行 JS 有没有报错?

do you get any JS error when you run it on IIS?

你在使用捆绑包吗?如果是这样,请检查问题是否是由于添加缩小/捆绑创建而发生的BundleTable.EnableOptimizations = true;RegisterBundles 方法中并在 VS 中重试.

are you using bundles? If so check if the problem happens because of minification/bundle creation adding BundleTable.EnableOptimizations = true; in RegisterBundles method and trying again in VS.

否则我建议您在本地 IIS 上运行(右键单击项目,选项卡Web",服务器"部分),看看它是否正常工作.

Otherwise I suggest you to run on local IIS (right click on project, tab "Web",section "Servers") and see if it works correctly.

根据您的路由,您应该在模板头中 <base href="@Url.Content("~/")"/> 并使用 $locationProvider.html5Mode(true); 关于路由配置.

Depending of your routing you should <base href="@Url.Content("~/")" /> in template head and get rid of "#" from url using $locationProvider.html5Mode(true); on route configuration.

如果以上都没有用,请在您的请求中添加更多详细信息.

If none of the above are useful please add more details to your request.

祝你有美好的一天,

阿尔伯托

这篇关于将站点托管到 IIS 后,AngularJS 路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆