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

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

问题描述

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

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

/v1/location/deviceid/appid

其他参数来自后车身".

Additional Parameter are coming from the Post-Body.

这一切对我来说都很好.现在,我通过允许将"deviceid"和/或"appid"和/或BodyData设置为null来扩展代码:

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:

可选参数必须在末尾"

"optional Parameters must be at the end"

下次尝试:

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

现在,即使调用发送了主体,我的函数AddNewLocation()也总是得到BodyData=null.

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.

Optional parameter BodyData is not supported by FormatterParameterBinding.

为什么要使用带有可选参数的解决方案?我的控制器仅通过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天全站免登陆