简体   繁体   中英

Asp.net Web Api query string parameter with dash

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; }
}

In the Controller , i have this action:

[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?

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).

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM