【问题标题】:Trouble getting a response from an asp.NET WebAPI in an Area无法从区域中的 asp.NET WebAPI 获取响应
【发布时间】:2012-07-18 01:51:27
【问题描述】:

我正在尝试将 ASP.NET Web API(来自 MVC 4)添加到我的项目中.....但是我在从 Area/WebAPI/Controller 获得任何响应时遇到了一些麻烦(不太确定在哪里出错了……)

我安装了 Route Debugger,如果我进入我的主页...我看到了路由...

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,它只是“Add New”生成的默认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

任何想法为什么?有什么需要配置的吗?

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    ASP.NET MVC 4 不支持开箱即用的 WebApi。

    Martin Devlers 提出了解决方案:ASP.NET MVC 4 RC: Getting WebApi and Areas to play nicely

    您还可以在我对类似问题的回复中获得更多详细信息(特别是对于便携式区域的支持): ASP.Net WebAPI area support

    【讨论】:

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

      完美运行。

      【讨论】:

      • 对不起,我没有包含完整的源代码......默认实现确实有一个 get(int id) 方法
      • 如果放在一个区域会怎样?
      猜你喜欢
      • 2018-02-20
      • 2012-06-19
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多