Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am performing angularjs routing in my asp.net application.I have a folder name template which consist of 3 html pages Home,About,Error. Here is my app.js file

 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 consist of

<div ng-controller="HomeController">
<h1>Home Page</h1>
<h3>{{Message}}</h3>

and so on.

In my Layout.cshtml file I have included the module myApp and base attribute.

   <!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <base href="/" />

Body has define some links

<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>

Routing working fine when I navigate from home to others . But when I refreshes about page it gives resource not found error.For this I have also included server side rewrite like this in my web.config file.

   <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>

enter image description here

Still the problem remains need helping in solving it . Thanks.

share|improve this question
    
The page at glance looks fine. Is your redirect rule working fine? PS: Ideally you should return landing page response for all urls than rewriting. In simple words if you redirect from /about to /home, then when you referesh about page you will end up with home page. – neolivz4ever May 19 '15 at 10:42
    
No when I refresh about page error come resource not found see my updated question – Ghazanfar May 19 '15 at 11:05
    
Your home url is /home its / so redirect to that. Your webserver does not know what it has to do with /home. Its defined in your angular app. – neolivz4ever May 19 '15 at 11:07
    
PS: I am not an expert in ASP and IIS so can't comment on that but it looks like it has nothing to do with angular it is your redirect logic which i feel is having a problem – neolivz4ever May 19 '15 at 11:09
1  
I have solved now see my answer thanks. – Ghazanfar May 19 '15 at 11:20
up vote 6 down vote accepted

I have solved it by making changes in Route.Config file

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

by replacing this with

 routes.MapRoute(
       name: "Application",
       url: "{*url}",
       defaults: new { controller = "Home", action = "Index" });
share|improve this answer
    
It works for me. But what I should do with logout form? javascript:document.getElementById('logoutForm').submit() It's ignored. – oyaebunterkrah Dec 1 '15 at 16:11

You have to redirect to your base URL (where your Angular App can start), not url="/home", but url="/"

share|improve this answer
    
Problem remains still – Ghazanfar May 19 '15 at 11:04

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.