Angularjs html5Mode页面刷新打破了asp.net mvc中的路由

前端之家收集整理的这篇文章主要介绍了Angularjs html5Mode页面刷新打破了asp.net mvc中的路由前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的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" });

猜你在找的Angularjs相关文章