与破折号Asp.net网页API查询字符串参数 [英] Asp.net Web Api query string parameter with dash

查看:165
本文介绍了与破折号Asp.net网页API查询字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有收到以下参数的 Web.Api 搜索方式:

I have an Web.Api search method which receives the following parameter:

[DataContract(Namespace = "", Name = "search")]
public class SearchParameters
{
    [DataMember(Name = "property-name")]
    public string PropertyName { get; set; }
}

控制器,我有这样的动作:

[HttpGet]
public void Search([FromUri]SearchParameters request)
{
    var parameters = 1;
}

我试图创造一个友好的名称属性名属性(属性名称),但模型绑定不认识的参数:

I am trying to create a friendly name for PropertyName property (property-name), but the model binder doesn't recognizes the parameter:

/api/changeLog/search?propertyName=asd  // Works

/api/changeLog/search?property-name=asd // Doesn't work

时的冲保留查询字符串的字符?或我使用它错了?

Is dash a reserved query string character? or i am using it wrong?

推荐答案

事实上,如 http://tool​​s.ietf.org/html/rfc3986 (标准页RFC 3986,它定义了统一资源标识符,也称为URI的)。

Indeed, it is reserved as defined in http://tools.ietf.org/html/rfc3986 (the standard page for RFC 3986, which defines Uniform Resource Identifiers, otherwise known as URI's).

修改我太迅速 - 在保留字符列表 - 我看错'='一'。看来,短线确实与毫无保留的字符列。这是没有意义的我,但,这只是我的意见了......这是RFC的官方标准,你会期望的服务器尊重格式 - 可能IIS做,但可能.NET不(见下文)。
编辑的结束

EDIT I was too quick -- I misread a '=' for a '-' in the list of reserved characters. It seems that the dash is indeed listed with the unreserved characters. It makes no sense to me but that is just my opinion... This RFC being the official standard, you would expect servers to respect the format -- and possibly IIS does, but probably .NET doesn't (see below). End of Edit

一般查询字符串按照格式项'和;'项目......在这里,你可以定义项作为变量'='的值。

Generally query strings follow the format ITEM '&' ITEM... where you can define ITEM as VARIABLE '=' VALUE.

但就像在编程语言中,变量不能包含使其难以解析的名称或将其映射到一个变量的任何字符。所以字符,如*和 - 不能用在变量名中使用。

But just like in programming languages, VARIABLE cannot contain any characters that make it hard to parse the name or to map it to a variable. So characters such as '*' and '-' cannot be used in variable names.

修改在.NET中的情况下,我希望,这是因为.NET试图查询字符串参数名称映射到一个方法参数名称。但是,那么他们就应该允许在 [数据成员] 属性使用破折号。由于这是不是这样的,我认为他们没有征询RFC时,他们建设这个功能......有没有人在意详细点吗?编辑的结束

EDIT In the case of .NET, I would expect that this is because .NET attempts to map a query string parameter name to a method parameter name. But then they should allow the use of a dash in the [DataMember] attribute. As this is not the case, I assume they did not consult the RFC when they were building this functionality... Would anyone care to elaborate? End of edit

对于相同的其他信息来源:

Other sources for the same information:

  • http://en.wikipedia.org/wiki/URI_scheme
  • http://en.wikipedia.org/wiki/Uniform_resource_identifier
  • http://www.rfc-editor.org/rfc/rfc3986.txt

这篇关于与破折号Asp.net网页API查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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