我将angularJS应用程序从xampp移动到ASP.NET Visual Studio。当我加载我的应用程序时,我收到了无法找到模板文件的错误消息。
我尝试了几种方法,但没有一种方法有效......有人知道问题在哪里吗?
文件层次结构的屏幕截图:
这里是包含模板的JS代码:
app.config(function ($routeProvider) {
$routeProvider
.when('/contacts',
{
controller: 'ContactsController',
templateUrl: '"/home/contacts.html'
})
.when('/add-contact',
{
controller: 'ContactAddController',
templateUrl: '/home/addContact.html'
})
.when('/edit-contact/:contactId',
{
controller: 'ContactEditController',
templateUrl: '/home/editContact.html'
})
.when('/display-contact/:contactId',
{
controller: 'ContactDetailsController',
templateUrl: '/home/displayContact.html'
})
.otherwise({ redirectTo: '/contacts' });
});
这是RouteConfig.cs(我保留原来我创建新项目时)
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
答案 0 :(得分:5)
如果您使用ASP.NET的默认路由机制(MVCx?)并要求该应用程序提供类似'/home/contacts.html'的网址,那么ASP.NET将查找文件称为 Home Controller.cs,它将在其中查找名为“contacts.html”的操作。
因为默认模式是{controller} / {action} / {id}。
向我们展示您的RouteConfig.cs。您可能需要一个规则来绕过ASP的路由并将视图作为静态文件提供,以使其适用于角度。
我认为view文件夹在MVC中有一个特殊功能,不能用于提供静态文件。 将.html文件放在另一种静态目录中并从那里尝试。
编辑:
如果您想要使用SPA方式,只需将您的html视图放在ASP项目根文件夹中的单独文件夹(例如:angularviews)中,并调整RouteConfig.cs,如下所示:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//exclude template folder from routing
routes.IgnoreRoute("angularviews/{*pathInfo}");
//add routes for api calls and stuff you need mapped to an asp controller here
/*
...
*/
//always deliver the main template for all requests
routes.MapRoute(
name: "SPA",
url: "{*catchall}",
defaults: new { controller = "Home", action = "Index" }
);
/*
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
*/
}
}
然后加载你的观点,例如'angularviews / view1.html'