【问题标题】:Routing with Web API使用 Web API 进行路由
【发布时间】:2015-01-06 23:43:05
【问题描述】:

这是我的控制器代码:

public class MenuController : ApiController
    {      
        [Route("api/menu/setorder"),HttpPost()]
        public HttpResponseMessage SetOrder(string orders)
        {
            //do stuff
        }
        return new HttpResponseMessage(HttpStatusCode.Accepted);
     }

这是WebApiConfig中的路由部分:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

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

当我尝试使用此代码调用操作时,出现 404 Not Found Error :

  var uri = 'api/menu/setorder';

        $(document).ready(function () {
            $.post(uri, { orders: "13_12-31:3" })
                .done(function (data) {                    
                    $.each(data, function (key, item) {
                        console.log(item["id"]);
                    });
                });
        });

我应该如何解决它?

【问题讨论】:

    标签: asp.net-web-api asp.net-web-api2 asp.net-web-api-routing


    【解决方案1】:

    查看此链接:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

    查看“按操作名称路由”部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 2014-01-13
      • 1970-01-01
      • 2012-09-28
      • 2021-07-06
      • 1970-01-01
      • 2014-06-09
      相关资源
      最近更新 更多