1

I'm using SPA technique using AngularJS in ASP.NET MVC framework,AngularJS routing between pages are working fine when it's run from VS2013,but after hosting the application in to IIS8.5 routing not working.

When i run the app using IIS express the url was http://localhost:12345/#/home After publishing http://localhost/myapp/#/home

Here is my app.js,

 $routeProvider.when("/home", {
        controller: "homeController",
        templateUrl: "/app/views/home.html"
    });

In index.html

<li> <a href="#/home">Home</a></li>

When i publish my app in windows azure, app is working fine. Because the url was myapp.azurewebsite.net. myapp.azurewebsite.net/#/home

How can i host similarly in local IIS.

1 Answer 1

1

You almost find find the answer -- that is all about absolute file path.

Quick fix:

Change templateUrl: "/app/views/home.html" to templateUrl: "app/views/home.html" (remove the heading slash)

Answer:

If you specify the templateUrl as /app/views/home.html, which starts with /, you tell the browser to get it from root.

If your webapp is stored in http://localhost/myapp/, your browser will visit http://localhost/app/views/home.html to get the template.

That also explains why your app works fine when it is places in root directory, but not working in sub directories, like http://localhost/myapp/#/home .

Do not worry about the side effect of relative path. All the resources are calculated relative to index.html.

Sign up to request clarification or add additional context in comments.

2 Comments

Superb. After removing / working fine :). Also for images..some of them it is working. for images i've used lazy loading feature... bn-lazy-src [bennadel.com/blog/2498-lazy-loading-image-with-angularjs.htm]. for this img type... again the same problem. Always showing default image.. even though image exists. Can you suggest.. Edit my question. added image.
@PrasadKanaparthi These images are stored outside the angular's directory? If so, the image path is irrelevant to the angular path, and they're uploaded to some backend? you may use absolute path for these files. and store images' base path using angular's constant to specify the backend's image storage directory.

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.