Angularjs html5Mode页面刷新打破了asp.net mvc中的路由
发布时间:2020-12-17 07:52:05 所属栏目:安全 来源:网络整理
导读:我在我的asp.net应用程序中执行angularjs路由.我有一个文件夹名称模板,包含3个html页面Home,About,Error. 这是我的app.js文件 var app = angular.module('myApp',['ngRoute','ngAnimate']);app.controller('HomeController',function ($scope) { $scope.Mess
我在我的asp.net应用程序中执行angularjs路由.我有一个文件夹名称模板,包含3个html页面Home,About,Error.
这是我的app.js文件 var app = angular.module('myApp',['ngRoute','ngAnimate']); app.controller('HomeController',function ($scope) { $scope.Message = "We are in home page"; }); app.controller('AboutController',function ($scope) { $scope.Message = "We are in about page"; }); app.controller('ErrorController',function ($scope) { $scope.Message = "404 page not found"; }); app.config(['$routeProvider','$locationProvider',function ($routeProvider,$locationProvider) { $routeProvider. when('/',{ redirectTo: function () { return '/home'; } }). when('/home',{ templateUrl: '/template/Home.html',controller: 'HomeController' }). when('/about',{ templateUrl: '/template/About.html',controller: 'AboutController' }). when('/error',{ templateUrl: '/template/Error.html',controller: 'ErrorController' }). otherwise({ redirectTo: '/error' }); $locationProvider.html5Mode(true); }]); Home.html由 <div ng-controller="HomeController"> <h1>Home Page</h1> <h3>{{Message}}</h3> 等等. 在我的Layout.cshtml文件中,我包含了模块myApp和base属性. <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <base href="/" /> 正文定义了一些链接 <body> <div> <ul> <li><a href="/home">Home</a></li> <li><a href="/about">About</a></li> <li><a href="/erro">Invalid Url</a></li> </ul> </div> <div class="container"> <div data-ng-view="" id="ng-view" class="slide-animation"></div> </div> 当我从家里导航到其他人时,路由工作正常.但是当我刷新页面时它会给资源找不到错误.为此我还在我的web.config文件中包含了这样的服务器端重写. <rewrite> <rules> <rule name="Main Rule" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/" /> </rule> </rules> </rewrite> 问题仍然是需要帮助解决它.谢谢.
我通过在Route.Config文件中进行更改来解决它
routes.MapRoute( name: "Default",url: "{controller}/{action}/{id}",defaults: new { controller = "Home",action = "Index",id = UrlParameter.Optional } ); 用这个代替 routes.MapRoute( name: "Application",url: "{*url}",action = "Index" }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |