0

So I am having an issue in setting up my angular routes.

Moving straight to the point, my angular routes defined don't hit my mvc controller and thus action methods.

The action method return partial views, which represent my templates.

Here is an image of my route configuration.

enter image description here

Here is an image of my controller actions.

enter image description here

I am sure I am missing something, but can't seem to figure out what.

2
  • any console error? Commented Jun 28, 2016 at 20:53
  • @PankajParkar, well I looked further around on stack overflow and came across one of you answers here : stackoverflow.com/questions/28351124/… in which you advised not to use the locationProvider as it will screw up the MVC routing and followed your advice. It seems like removing the locationProvider, or setting it to false fixes the issue. I am still to investigate why this happens Commented Jun 28, 2016 at 21:38

2 Answers 2

0

This example helps you to understand better about $routeProvider and $locationProvider.

The only issue I see are relative links and templates not being properly loaded because of this.

from the docs regarding HTML5 mode

Be sure to check all relative links, images, scripts etc. You must either specify the url base in the head of your main html file () or you must use absolute urls (starting with /) everywhere because relative urls will be resolved to absolute urls using the initial absolute url of the document, which is often different from the root of the application. In your case you can add a forward slash / in href attributes ($location.path does this automatically) and also to templateUrl when configuring routes. This avoids routes like example.com/tags/another and makes sure templates load properly.

Here's an example that works:

<div>
    <a href="/">Home</a> | 
    <a href="/another">another</a> | 
    <a href="/tags/1">tags/1</a>
</div>
<div ng-view></div>

And

app.config(function($locationProvider, $routeProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider
    .when('/', {
      templateUrl: '/partials/template1.html', 
      controller: 'ctrl1'
    })
    .when('/tags/:tagId', {
      templateUrl: '/partials/template2.html', 
      controller:  'ctrl2'
    })
    .when('/another', {
      templateUrl: '/partials/template1.html', 
      controller:  'ctrl1'
    })
    .otherwise({ redirectTo: '/' });
});

If using Chrome you will need to run this from a server.

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

Comments

0

Well what worked for me was to remove the setting for the $locationProvider.html5Mode. As someone mentioned in another stack overflow post, here MVC5 and Angular.js routing - URLs not matching using the locationProvider in MVC seems to screw up the routing. I am still to investigate why exactly this happens, as all I thought it did was remove the '#' in the url, but seems like there's more to it

Comments

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.