Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am writing my blog site with using angular.js and asp.net webapi. I did url routing with angular.js. when I click url but I get 404 not found error. How to fix this problem?

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 page

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>
share|improve this question
    
teplateUrl: 'blogpost.html', should be templateUrl: 'blogpost.html', & angular doesn''t provide an direct access to html file –  Pankaj Parkar Sep 5 at 10:30
    
i changed but error continues –  Batuhan Avlayan Sep 5 at 10:53
    
mvc views folder doen't provide an access to the html files directly..from where you are trying to access the html file? –  Pankaj Parkar Sep 5 at 10:57
    
There are two projects in my solution. First BlogSite.API, Second BlogSite.UI. I didn't create blogsite.ui with mvc template. blogpost.html file is in blogsite.ui project –  Batuhan Avlayan Sep 5 at 11:09
    
is there any error change in console? or you are getting the same error were getting before? –  Pankaj Parkar Sep 5 at 11:10

1 Answer 1

up vote 0 down vote accepted

You anchor should be having # to restrict redirection, putting # at the start of URL will keep you in same page & will keep you in SPA

<a href="#post/{{blogPost.PostUrl}}" class="lastPostUrl mainPagePostTitle">
  {{blogPost.PostTitle}}
</a>
share|improve this answer
    
Thanks a lot :) error solved –  Batuhan Avlayan Sep 5 at 11:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.