2

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.

9
  • 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. Commented May 19, 2015 at 10:42
  • No when I refresh about page error come resource not found see my updated question Commented May 19, 2015 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. Commented May 19, 2015 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 Commented May 19, 2015 at 11:09
  • I have edited that in my code now url is ="/" still same problem Commented May 19, 2015 at 11:09

2 Answers 2

13

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" });
Sign up to request clarification or add additional context in comments.

3 Comments

It works for me. But what I should do with logout form? javascript:document.getElementById('logoutForm').submit() It's ignored.
I find that when I hit refresh, the proper view appears again...but the route is dropped from the URL - so a 2nd refresh loads my main page again. For instance I'm at localhost/home (hit F5) home is reloaded, but the url in the browser changes to localhost/ (hit F5) main page reloaded. Any ideas?
anyone using Asp.Net Core: the url param is now called template
0

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

1 Comment

Problem remains still

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.