AngularJS控制器没有达到webapi

时间:2015-09-21 19:17:14

标签: angularjs asp.net-web-api

为了在使用angularjs行走之前进行爬行,我创建了一个简单的应用程序来使用webapi显示来自sql server表的结果。不幸的是,webapi永远不会被调用,因为路由错误,但我不知道如何解决。提琴手显示404错误。

cshtml如下,定义应用程序和控制器。

<script type="text/javascript">

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

app.controller('streamController', function($scope, $http){

    $scope.loading = true;
    $scope.addMode = false;

    //Used to display the data   
    $http.get('/api/Stream/').success(function (data) {
        $scope.streams = data;
        $scope.loading = false;
    })
    .error(function () {
        $scope.error = "An Error has occured while loading streams!";
        $scope.loading = false;
    });

});

</script>

cshtml文件中的呈现部分是

<div data-ng-app="streamApp" data-ng-controller="streamController" class="container">
....
</div>

webapi类位于MVC项目中名为WebApi的文件夹中,但由于从未到达过,因此无需显示其代码。无论如何它都是非描述性的。

路由配置如下:

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

    }
}

我不确定是否需要在angularjs代码或mvc route config中指定路由指令,以及确切要提供哪些功能或配置。我已经尝试将webapi类移动到Controllers文件夹,结果相同 - http 404.有关如何使这个示例获得webapi代码的任何建议都将不胜感激。

好热的......我通过在我的webapi类方法中添加注释解决了眼前的问题

    [Route("api/stream")]
    [System.Web.Http.HttpGet]
    public IEnumerable<Models.StreamViewModel> Get()
    {
       .....
    }

所以现在问题是应该用RouteConfig作为更好的做法吗?或者Route()注释是首选方式吗?或者是另外六个人中的6个?

3 个答案:

答案 0 :(得分:1)

回答您的最新问题,路线的最佳做法

Route()注释是执行此操作的首选方式。

  

MVC 5支持一种新类型的路由,称为属性路由。作为   name暗示,属性路由使用属性来定义路由。   通过属性路由,您可以更好地控制Web中的URI   应用

     

早期的路由风格,称为基于约定的路由,是   仍然完全支持。事实上,你可以将两种技术结合起来   同一个项目。

属性路由还有其他优点,例如

  
      
  1. 它将路径信息放在控制器操作的旁边   实现那条路线。这有助于调试和故障排除,   以及提供快速搜索路线的能力   解决方案中的信息。

  2.   
  3. 降低了路线变更过程中的风险。在RouteConfig.cs或WebApiConfig.cs中(在Web API解决方案的情况下),   存在无意中改变错误路线的可能性   否则会对您申请的其他部分产生不利影响。

  4.   
  5. 您可能还希望包含可接受的HTTP方法,允许的用户类型和注册优先级,如果包含在内   基于属性的路由,将所有这些信息放在一起   的地方。

  6.   

这篇文章为我提供了上述灵感和强化,并详细介绍: http://kevinmontrose.com/2011/07/25/why-i-love-attribute-based-routing/

答案 1 :(得分:0)

您可以使用其中任何一种,但如果没有注释,您的端点将是api/get而不是api/stream(假设您没有重命名您的方法)。

答案 2 :(得分:0)

首先,你应该使用ApiController而不是Controller,因为它扮演api动作的角色。

其次,如果我们看一下,看起来你创建了一个名为ApiController的控制器和一个名为Stream的函数。否则,它误解了使用MVC设计Web集成。

App_Start \ WebApiConfig.cs:

using System.Web.Http;

class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{action}/{id}",
            new { id = RouteParameter.Optional });
    }
}

的Global.asax.cs:

using System.Web.Http;

...

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

和ApiControllerNameController:// Controller字会关闭文件名,无需编写即可到达:

using System;
..
..
..

namespace MvcApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values/MethodName
        public IEnumerable<int> MethodName()
        {
            return new List<int> { 1, 2, 3 };
        }
    }
}
相关问题