0

I'm developing an application in .net mvc with angularjs. When I don't use html5 mode it works fine, but when I set html5 mode to true the server calls that address giving me an he resource cannot be found.

This is my app.js

var app = angular.module("myApp", ['ngRoute'])
.config(function ($routeProvider, $locationProvider) {
    $routeProvider.when('/test',
    {
        templateUrl: 'templates/TestPage.html',
        controller: 'ProfesionalController'
    });

    $routeProvider.otherwise({ redirectTo: '/' });

    $locationProvider.html5Mode(true);


});

And this is my view (layout):

<!DOCTYPE html>
<script src="~/Scripts/angular/angular.js"></script>
<script src="~/Scripts/angular/angular-route.js"></script>

<script src="~/app/app.js"></script>
<script src="~/app/home/home.js"></script>
<script src="~/app/profesional/profesional.js"></script>
<base href="/">
         <a href="test">Test</a>
         @*this is the link*@

        </div>
    </div>
</div>
<div ng-app="myApp" class="container body-content">
    <div ng-view></div>
    @RenderBody()

</div>

When I click to test link the server try to locate: http://localhost:39881/test" giving me an 404 Not Found - http://localhost:39881/test"

What I'm missing. It works well with no html5 mode.

6
  • Post your controller. Commented Jul 28, 2015 at 19:54
  • possible duplicate of asp.net mvc hosting angular app with html5mode and routing Commented Jul 28, 2015 at 19:55
  • The angular controller i'ts not big thing: app.controller('ProfesionalController', function ($scope) { $scope.profesional= 'hi Customer!'; }); Commented Jul 28, 2015 at 19:59
  • I meant your MVC controller. Commented Jul 28, 2015 at 20:12
  • The MVC controller it's the default index controller: public class CustomerController : Controller { // // GET: /Customer/ public ActionResult Index() { return View(); } } Commented Jul 28, 2015 at 20:15

1 Answer 1

2

In your mvc route config catch all url's and redirect to your index page. It will help.

   routes.MapRoute(
      name: "AngularCatchAll",
      url: "{*url}",
      defaults: new { controller = "Home", action = "Index" });
Sign up to request clarification or add additional context in comments.

4 Comments

With a catchall, how do you route JSON posts/requests to the proper controller?
Configure mvc routing it's in case you hit refresh located in an angular route don't get a 404, because it will hit the server with an unknown url. If you use the links that's not necessary.
In this case I'm using WebAPI controllers to post/get data, with different routing. I'm dont use MVC Controllers at all. All routing and templating doing by Angular.
Worked for me (API calls were not affected). Only needed to replace url: with template:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.