Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何在mvc4中从url调用web api_Asp.net_Asp.net Mvc 4_Asp.net Web Api - Fatal编程技术网

Asp.net 如何在mvc4中从url调用web api

Asp.net 如何在mvc4中从url调用web api,asp.net,asp.net-mvc-4,asp.net-web-api,Asp.net,Asp.net Mvc 4,Asp.net Web Api,我创建了一个带有两个参数的web api。我想从url测试它是否工作。我试过了,但没能打电话。我的密码是 API路由 public static void Register(HttpConfiguration config) { config.EnableCors(); config.Routes.MapHttpRoute( name: "DefaultApi", routeT

我创建了一个带有两个参数的web api。我想从url测试它是否工作。我试过了,但没能打电话。我的密码是

API路由

    public static void Register(HttpConfiguration config)
    {           
        config.EnableCors();

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


[HttpGet]
    public List<Library> GetLibrary(string DataType, int DataId)
    {
        List<Library> LibraryList = new List<Library>();
        if (DataType == "Course")
        {
            using (ICA.LMS.Service.Models.Entities dbCourse = new Models.Entities())
            {
                LibraryList = (from c in dbCourse.COURSELIBRARies
                               where c.LIBITEMID == DataId
                               select new Library { Id = c.LIBITEMID, Name = c.GROUPNAME, Desc = c.DESCRIPTION }).ToList();
            }
        }
        return LibraryList;
    }
结果我得到

<Error>
   <Message>
       No HTTP resource was found that matches the request URI     'http://localhost:1900/api/librarybyid/?DataType='Course'&DataId=1'.
   </Message>
   <MessageDetail>
      No type was found that matches the controller named 'librarybyid'.
    </MessageDetail>
</Error>

未找到与请求URI名称匹配的HTTP资源http://localhost:1900/api/librarybyid/?DataType='课程'&DataId=1'。
未找到与名为“librarybyid”的控制器匹配的类型。

请使用代码中的控制器名称和api路由更新帖子。您是否将控制器命名为“librarybyidController”?因为错误消息清楚地显示“没有找到与名为'librarybyid'的控制器匹配的类型”,所以我将其命名为'librarybyidController'。那么我该如何从网络浏览器中调用它呢?您能发布控制器的完整源代码吗。我用一个简单的方法尝试了一个简单的Web API服务(与您的示例中的方法类似),它工作得很好。您的Web API控制器中还有其他方法吗?
<Error>
   <Message>
       No HTTP resource was found that matches the request URI     'http://localhost:1900/api/librarybyid/?DataType='Course'&DataId=1'.
   </Message>
   <MessageDetail>
      No type was found that matches the controller named 'librarybyid'.
    </MessageDetail>
</Error>