ASP.NET Web API中的参数绑定

编程入门 行业动态 更新时间:2024-10-11 03:23:13
本文介绍了ASP.NET Web API中的参数绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的Web API中有一个用于身份验证(HMAC)的DelegatingHandler.

I have a DelegatingHandler in my web API for authentification (HMAC).

我想在请求中添加一个GET参数,以将用户的ID返回给我的控制器.

I would like to add a GET parameter to the request to return the user's id to my controller.

在处理程序中,我尝试像这样添加它:

In my handler, I tried adding it like so:

public class SecurityHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { string apikey = request.Headers.GetValues(AuthConfig.ApiKeyHeader).First(); UserDTO user = UserRepo.GetUser(apikey); if (user == null) { return SendResponseText("Invalid API key"); } // Validate signature ... // Add user Id to the URI request.RequestUri = new Uri(request.RequestUri.OriginalString + "&UserId=" + user.Id); return base.SendAsync(request, cancellationToken); } }

在我的控制器中,我可以从请求uri中获取新添加的参数,但是参数绑定无效

In my controller, I'm able to get the newly added parameter from the request uri, however the parameter binding is not working

public class MyModel { public int UserId { get; set; } ... } public string Get([FromUri] MyModel model) { // model.UserId has not been set (= 0) // Request.RequestUri contains "&UserId=5" }

更新

我猜想绑定是通过HttpContext中的Params完成的.我尝试过类似的操作,但是Params集合是只读的.

I'm guessing the binding is being done from the Params in the HttpContext. I tried something like this, but Params collection is readonly.

var newContext = request.Properties["MS_HttpContext"] as HttpContextWrapper; newContext.Request.Params.Add("UserId", "8"); request.Properties.Remove("MS_HttpContext"); request.Properties.Add("MS_HttpContext", newContext);

推荐答案

我已经在我的身边尝试过,并且可以正常工作.

I have tried at my side and it working.

这是我的示例网址.

localhost:50382/api/values?UserId=10

这是控制器动作.

public class ValueController : ApiController { public IEnumerable<string> Get([FromUri]Model my) { return new string[] { "value1", "value2" , my.UserId.ToString() }; } }

根据您在此处的评论,我创建了委托处理程序.

As per your comment here I created delegate handler.

public class MyMessageHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { var myTempObj = new { id = 20 }; /* You have to check this and remove this key. If key present that FromUri use cached properties*/ if (request.Properties.ContainsKey("MS_QueryNameValuePairs")) { request.Properties.Remove("MS_QueryNameValuePairs"); } // Now prepare or update uri over here. It will surely work now. if (!string.IsNullOrEmpty(request.RequestUri.Query) && request.RequestUri.Query.Contains('?')) { request.RequestUri = new Uri(request.RequestUri + "&UserID=" + myTempObj.id); } else { request.RequestUri = new Uri(request.RequestUri + "?UserID=" + myTempObj.id); } return base.SendAsync(request, cancellationToken); } }

这是预期的工作.

如果您的原始请求包含用户ID,则它将被复制,并且将无法使用.返回0.

If your original request contain userid then it get duplicated and it will not work. It return 0.

我也怀疑您使用的是request.RequestUri.OriginalString.只需尝试使用request.RequestUri.可能是OriginalString具有编码值.

I also have doubt that you are using request.RequestUri.OriginalString. Just try to use request.RequestUri. It might possible that OriginalString has encoded value.

更多推荐

ASP.NET Web API中的参数绑定

本文发布于:2023-11-10 14:30:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575631.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   参数   NET   ASP   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!