Asp.net MVC使用[FromUri]将url参数解析为对象 [英] Asp.net MVC parse url parameter to object using [FromUri]

查看:276
本文介绍了Asp.net MVC使用[FromUri]将url参数解析为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在为一些Url-Parameter苦苦挣扎.我尝试从以下网址创建对象:

Currently I'm struggeling with some Url-Parameter. I try create an object from the following url:

../Data/GetOrderList?take=10&skip=0&page=1&pageSize=10&sort[0][field]=Password&sort[0][dir]=asc

除了 sort 部分之外,其余部分的效果都很好.我的方法如下:public async Task<HttpResponseMessage> GetOrderList([FromUri]Models.GridParameter model)

Which works pretty will except, the sort part. My method looks like this: public async Task<HttpResponseMessage> GetOrderList([FromUri]Models.GridParameter model)

问题是GridParameter类,其中sort应该是其中的属性.但是我不知道如何将这部分&sort[0][field]=Password&sort[0][dir]=asc表示为对象.

The problem is the GridParameter class, where sort should be a property in. But I don't knwo how to represent this part &sort[0][field]=Password&sort[0][dir]=asc as an object.

有人以前解决过这个问题吗?

Does someone solved this issue before?

非常感谢!

推荐答案

我假设您使用的是For ASP.NET MVC 5,不是WebAPI对吗? 最简单但不是最好的方法是在操作方法中从Request.QueryString读取查询字符串参数.

I'm assuming you are using For ASP.NET MVC 5, not WebAPI right? The easiest but not nicest way would be to read query string parametrs from Request.QueryString in your action method.

您还可以编写自己的

You can also write your own ModelBinderAttribute class and parse Query string there, that would be much cleaner solution.

这篇关于Asp.net MVC使用[FromUri]将url参数解析为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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