【问题标题】:WebApi using get parameter in middle of uriWebApi 在 uri 中间使用 get 参数
【发布时间】:2015-03-09 03:56:34
【问题描述】:

你好,新手 - 使用 VS 2013 ASP.net WebApi - 有以下 uri 的规范:

/device/{deviceId}/register

尝试了一些方法,例如:

public class DeviceController : ApiController
{
        [Route("/{deviceId}/register")]
        [HttpGet]
        public async Task<IHttpActionResult> Register(string deviceId)
        {
        }
}

并尝试更新RouteConfig.RegisterRoutes:

routes.MapRoute(
name: "Device",
url: "device/{deviceid}/",
defaults: new { deviceId = "", action = "Register"}
);

【问题讨论】:

  • 改用[Route("device/{deviceId}/register")]。您的属性路由和显式配置路由是相互独立的。

标签: c# asp.net-web-api routes


【解决方案1】:

您应该将 Route 属性调用更改为:

[Route("device/{deviceId}/register")]

并删除您在 RouteConfig.RegisterRoutes 中所做的所有更改 - 它们不是必需的。

然后它会正常工作。属性路由与 HttpConfiguration 对象中定义的路由无关。

【讨论】:

    猜你喜欢
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多