Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am trying to use ngRoute to link to an html and/or cshtml document in my ASP.NET MVC 5 project:

/Views/Home/index.cshtml:

<ul class="pager">
    <li><a href="#/route1">Route 1</a></li>
    <li><a href="#/route2">Route 2</a></li>
</ul>
<div ng-view></div>

I've tried so many ways to type the file-path that I'm convinced it's the way that ASP.NET MVC handles the routing and views, something that I've recently learned here. Here's my latest attempt at the templateUrl element:

/Scripts/app/app.js:

var app = angular.module('app', ['ngRoute']);

app.config(['$routeProvider',
    function ($routeProvider) {
        $routeProvider.
            when('/route1', {
                templateUrl: '/Views/Modules/page1.html',
                controller: 'RouteController'
            }).
            when('/route2', {
                templateUrl: '/Views/Modules/page2.html',
                controller: 'RouteController'
            }).
            otherwise({
                redirectTo: '/'
            });
    }]);

app.controller("RouteController", function ($scope) {

});

Just in case it's needed, here's my HomeController.cs

/Controllers/HomeController.cs:

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

    public ActionResult page1()
    {
        return View();
    }

    public ActionResult page2()
    {
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

Chrome's console tells me that app.js's references to page1 and page2 are inaccurate, no matter what I do. Could someone help me understand what's going on?

share|improve this question
1  
Ok, to start with, ASP.Net MVC and Angular.js have full routing engines. It can be very difficult to define routes in one which will be actually handled by the other. it would be helpful to understand what you are expecting to happen. Right now, you have angular looking on the server for /Views/Modules/, and the ActionResult() methods in C# aren't involved. is this what you are expecting? –  Claies Nov 12 '14 at 22:48
    
@AndrewCounts This is what I'm trying to understand. I'm fairly new to both AngularJS and (a bit less to)ASP.NET MVC. I'd like to have app.js grab the info from the .html file. This is possible, right? –  Laserbeak43 Nov 12 '14 at 22:53
    
if your page1.html and page2.html files are both in the appropriate directory (/Views/Modules/ in your example) then they will be loaded by Angular without the need for any C# methods. C# won't even be called, only IIS. –  Claies Nov 12 '14 at 22:56
    
I've checked countless times. Do you think it's my syntax in the script? Because when I use it in Webstorm(it's a simple project I just copy the html files and app.js) I can point to it with no problems. /Scripts is a directory just sub of root,and /app is directly under that, but "../../Views/Modules/page1.html" returns an error in the Chrome console as well. –  Laserbeak43 Nov 12 '14 at 23:05
1  
you are pointing to the server root here, those pages would be at something like http://yourserver.com/Views/Modules/page1.html. They are not relative to your JavaScript files. if they are in the /scripts/app/Views/Modules/ directory, you have to reference it that way, using ../../ won't work in this context. –  Claies Nov 12 '14 at 23:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.