2012-04-23 38 views
1

我想添加的ASP.NET Web API(从MVC 4)我的项目.....但我有一些麻烦的任何从区域/的WebAPI /控制器响应(不太肯定它会错了...)麻烦从asp.NET的WebAPI的响应在一个地区

我装的路线调试器,如果我去我的主网页...我看到的路线......

Matches Current Request Url Defaults Constraints DataTokens 


    False api/{controller}/{action}/{id} action = Index, id = UrlParameter.Optional (empty) Namespaces = OutpostBusinessWeb.Areas.api.*, area = api, UseNamespaceFallback = False 
    False {resource}.axd/{*pathInfo} (null) (empty) (null) 
    True {controller}/{action}/{id} controller = Home, action = Index, id = UrlParameter.Optional (empty) (empty) 
    True {*catchall} (null) (null) (null) 

所以好像途径是建立

接下来我在“API”片区PlansController,这只是默认apiController基因通过 “新增” 评为...

public class PlansController : ApiController 
{ 
    // GET /api/<controller> 
    public IEnumerable<string> Get() 
    { 
     return new string[] { "value1", "value2" }; 
    } 

    // GET /api/<controller>/5 
    public string Get(int id) 
    { 
     return "value"; 
    } 

    // POST /api/<controller> 
    public void Post(string value) 
    { 
    } 

    // PUT /api/<controller>/5 
    public void Put(int id, string value) 
    { 
    } 

    // DELETE /api/<controller>/5 
    public void Delete(int id) 
    { 
    } 
} 

现在,当我去http://localhost:2307/api/Plans/1

我得到

Server Error in '/' Application. 
The resource cannot be found.  
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. 
    Requested URL: /api/Plans/1 

任何想法,为什么?有什么我需要配置?

回答

2

将其更改为:与

// GET /api/<controller> 
    public IEnumerable<string> GetMultiple(int id) 
    { 
     return new string[] { "value1", "value2" }; 
    } 

叫它:

http://localhost:2307/api/Plans/GetMultiple/1

这是我的Global.asax:

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

我的控制器:

public class MyApiController : ApiController 
    { 
     public IQueryable<MyEntityDto> Lookup(string id) { 

     .. 
    } 

我把它叫做如下:

http://localhost/MyWebsite/api/MyApi/Lookup/hello 

完美的作品。

+0

对不起,我不包括完整的源....默认的实现确实有GET(INT ID)的方法 – 2012-04-23 23:49:49

+0

更新我的问题,以显示全班 – 2012-04-23 23:50:58

+0

请看到我的编辑 – 2012-04-23 23:53:09