Search code examples
asp.netangularjsangularjs-routing

Angular-ASPnet - Routing Failed


After authentication, I need access to my main page. My controller Angular:

$window.location.href = 'Home/Index';

And arrived in my index page, I get the error below: enter image description here

I do not understand this error my URL is correct. thanks More details :

I have a start page that serve me to login(RouteConfig.cs) :

      name: "Default",
                url: "{controller}/{action}/{id}",
               defaults: new { controller = "Login", action = "Authentification", id = UrlParameter.Optional }

Once authenticated, I have to go to the main page of the application that contains several pages of content, the main page and the login page are different modules. Here is the script to go to my main page: And in my main page, which should be displayed first is the homepage according to the script below:

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

My problem is that I get to see my main page but I do not get to have the content page HomePage ". (Error 404 failed to load template)

But if I change RouteConfig.cs , its OK:

 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
               defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
               
            );

My problem is the passage from my login page to my main page. ASP controllers are aLL OK

Config $rootProvider :

var MainApp = angular.module("MainApp",
    [
        "ngRoute",
        "GlobalParamServices",
        "HomePageApp",
        "ListCollaborateurApp",
        "CollaborateurServices",
        "ShowEditCollaborateurApp",
        "AddNewCollaborateurApp",
        "ListRoleApp",
        "ShowEditRoleApp",
        "AddNewRoleApp"
    ]);

//Showing Routing
MainApp.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    debugger;
    //Main
    $routeProvider.otherwise(
                    {
                        redirectTo: '/HomePage'
                    });
    $routeProvider.when('/HomePage',
                        {
                            templateUrl: 'Home/HomePage',
                            controller: 'HomePageController'
                        });
    //Collaborateur
    $routeProvider.when('/ListCollaborateur',
                        {
                            templateUrl: 'Collaborateur/ListCollaborateur',
                            controller: 'ListCollaborateurController'
                        });
    $routeProvider.when('/AddNewCollaborateur',
                        {
                            templateUrl: 'Collaborateur/AddNewCollaborateur',
                            controller: 'AddNewCollaborateurController'
                        });
    $routeProvider.when('/ShowEditCollaborateur',
                       {
                           templateUrl: 'Collaborateur/ShowEditCollaborateur',
                           controller: 'ShowEditCollaborateurController'
                       });

    //Role
    $routeProvider.when('/ListRole',
                   {
                       templateUrl: 'Role/ListRole',
                       controller: 'ListRoleController'
                   });
    $routeProvider.when('/AddNewRole',
                    {
                        templateUrl: 'Role/AddNewRole',
                        controller: 'AddNewRoleController'
                    });

    $routeProvider.when('/ShowEditRole',
                      {
                          templateUrl: 'Role/ShowEditRole',
                          controller: 'ShowEditRoleController'
                      });



    //$locationProvider.html5Mode(true).hashPrefix('!')
}]);

Do you have a solution to this case? Thank you,


Solution

  • Angular is complaining about not being able to load the template from the server. You need to add the action in your HomeController for HomePage in order for ASP.NET MVC to return a view.

    public class HomeController : Controller
    {
        public ActionResult HomePage()
        {
            return View();
        }
    }