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?
/Views/Modules/
, and theActionResult()
methods in C# aren't involved. is this what you are expecting? – Claies Nov 12 '14 at 22:48page1.html
andpage2.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:56http://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