簡體   English   中英

在asp.net中找不到angular.js路由404錯誤

[英]angular.js routing 404 not found error in asp.net

我正在使用angular.js和asp.net webapi編寫我的博客網站。 我使用angular.js進行了網址路由。 當我單擊url但出現404 not found錯誤時。 如何解決這個問題?

var blogSite = angular.module("blogSite", ["ngRoute"]);
blogSite.config(['$routeProvider', function ($routeProvider) {
$routeProvider
    .when("post/:post", {
        teplateUrl: 'blogpost.html',
        controller: 'blogPostController'
    })
    .otherwise({
        redirectTo: 'index.html'
    });
}]);

blogSite.controller("mainPageController", function ($scope, $http,     $routeParams) {
var url = baseUrl + "getpost";
$http.get(url)
    .success(function (data, status, headers, config) {
        if (data.length > 0) {
            $scope.LastBlogPosts = data;
        }
        else {
            $scope.LastBlogPosts = null;
        }
    })
    .error(function (data, status, headers, config) {
        console.log(status);
    });
});

blogSite.controller("blogPostController", function ($scope, $http, $routeParams) {
 $scope.TestMessage = "Test Message"
});

index.html頁面

div ng-controller="mainPageController">
            <ul>
                <li ng-repeat="blogPost in LastBlogPosts">
                    <a href="post/{{blogPost.PostUrl}}" class="lastPostUrl mainPagePostTitle">{{blogPost.PostTitle}}</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<div ng-view>

</div>

您的錨點應具有#限制重定向,將#放在URL的開頭將使您進入同一頁面,並使您進入SPA

<a href="#post/{{blogPost.PostUrl}}" class="lastPostUrl mainPagePostTitle">
  {{blogPost.PostTitle}}
</a>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM