如何在ASP web api项目中插入AngularJS项目

时间:2015-03-03 20:56:09

标签: asp.net angularjs asp.net-web-api asp.net-mvc-routing

我有一个asp web API项目和相关的AngularJS项目 我知道如果我想给Angular外观,我应该致电index.html所以

  1. 如何将Angular的index.html文件作为默认页面调用?
  2. 在ASP Web API项目中导入Angular文件夹的最佳位置?
  3. 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 }
            );
        }
    

    WebApiConfig的艺术:

    config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    

1 个答案:

答案 0 :(得分:1)

如果你想使用angularjs路由设置你的routeConfig如下:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "App",
            url: "App/{*catchall}",
            defaults: new { controller = "App", action = "Index" });

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "App", action = "Index", id = UrlParameter.Optional }
        );

在index.cshtml视图中,您需要在HTML标记上定义您的应用,并添加您的ng-view指令:

<html ng-app="appModule">
   <div ng-view>
   </div>
</html>

这将让应用程序继续运行。确保在index.cshtml文件中包含所有.js文件。

在app(mvc)控制器中,我们为角应用程序引导一些数据。你可以随心所欲地做到这一点。