【问题标题】:How to Insert AngularJS project in an ASP web api Project如何在 ASP web api 项目中插入 AngularJS 项目
【发布时间】:2015-03-03 20:56:09
【问题描述】:

我有一个 ASP Web API 项目和相关的 AngularJS 项目。
我知道如果我想给 Angular 出场,我应该打电话给index.html 所以

  1. 如何将 Angular 的 index.html 文件称为默认页面?
  2. 在 ASP Web API 项目中导入 Angular 文件夹的最佳位置?

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 }
        );

【问题讨论】:

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


【解决方案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 文件。

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

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多