Web Api 属性路由中的可选参数 [英] Optional Parameters in Web Api Attribute Routing

查看:37
本文介绍了Web Api 属性路由中的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想处理以下 API 调用的 POST:

I want to handle POST of the following API-Call:

/v1/location/deviceid/appid

附加参数来自 Post-Body.

Additional Parameter are coming from the Post-Body.

这对我来说一切正常.现在我想通过允许deviceid"和/或appid"和/或 BodyData 为空来扩展我的代码:

This all works fine for me. Now I wnat to extend my code by allowing "deviceid" and/or "appid" and/or BodyData to be null:

/v1/location/deviceid
/v1/location/appid
/v1/location/

这 3 个 URL 应该通过相同的路由响应.

These 3 URLs should responded by the same route.

我的第一种方法(需要 BodyData):

My first approach (BodyData required):

[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
public location_fromuser Post(string deviceid = null, string appid = null, [FromBody] location_fromuser BodyData)
{
    return repository.AddNewLocation(deviceid, appid, BodyData);
}

这不起作用并返回编译错误:

This does not work and returns a compile error:

可选参数必须在最后"

下次尝试:

[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
public location_fromuser Post([FromBody] location_fromuser BodyData, string deviceid = null, string appid = null)

现在我的函数 AddNewLocation() 总是得到一个 BodyData=null - 即使调用发送了 Body.

Now my function AddNewLocation() get always an BodyData=null - even if the call send the Body.

最后我将所有 3 个参数都设置为可选:

Finally I set all 3 Parameter optional:

[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
public location_fromuser Post(string deviceid = null, string appid = null, [FromBody location_fromuser BodyData = null)

不工作:

FormatterParameterBinding 不支持可选参数 BodyData.

为什么我需要带有可选参数的解决方案?我的控制器仅通过 POST 处理添加新位置".

Why do I want a solution with optional Parameters? My Controller handles just the "adding of a new Location" via a POST.

我想发送错误数据我自己的异常或错误消息.即使调用有缺失值.在这种情况下,我希望能够决定通过我的代码抛出异常或设置默认值.

I want to send on wrong data my own exceptions or error messages. Even if the call has missing values. In this case I want to be able to decide to throw an exception or Setting Defaults by my code.

推荐答案

对于像 /v1/location/1234 这样的传入请求,可以想象 Web API 很难自动计算如果与1234"对应的段的值与appid相关而不与deviceid相关,则输出.

For an incoming request like /v1/location/1234, as you can imagine it would be difficult for Web API to automatically figure out if the value of the segment corresponding to '1234' is related to appid and not to deviceid.

我认为您应该将您的路线模板更改为[Route("v1/location/{deviceOrAppid?}", Name = "AddNewLocation")] 然后解析 deiveOrAppid 以找出 id 的类型.

I think you should change your route template to be like [Route("v1/location/{deviceOrAppid?}", Name = "AddNewLocation")] and then parse the deiveOrAppid to figure out the type of id.

此外,您还需要将路线模板中的段设为可选,否则这些段将被视为必需的.请注意本例中的 ? 字符.例如:[Route("v1/location/{deviceOrAppid?}", Name = "AddNewLocation")]

Also you need to make the segments in the route template itself optional otherwise the segments are considered as required. Note the ? character in this case. For example: [Route("v1/location/{deviceOrAppid?}", Name = "AddNewLocation")]

这篇关于Web Api 属性路由中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆