Morix Dev
Morix Dev

Reputation: 2744

ASP.NET MVC URI parameters naming

I am working at an ASP.NET MVC Web Api controller, and I am just a beginner so maybe my question is quite trivial...

I'd like to take a parameter named $filter as input to my controller's method:

[Route("List")]
[HttpGet]
public IHttpActionResult List([FromUri] string $filter = null)
{
    // ... code here ...

    return (Ok());
}

However I can't do like that, because $filter is not a valid C# variable name!

Anyway (due to customer requests) I really need to invoke that method passing $filter=something in the URI...

How can I do? Is there a method to "map" the naming of URI parameters, so that in code I can use the variable name filter (without the dollar) and then instructing the MVC layer to map $filter onto filter?

Upvotes: 0

Views: 681

Answers (1)

Christian Gollhardt
Christian Gollhardt

Reputation: 17024

Set the Name property of FromUriAttribute:

[Route("List")]
[HttpGet]
public IHttpActionResult List([FromUri(Name = "$filter")] string filter = null)
{
    // ... code here ...

    return (Ok());
}

Upvotes: 6

Related Questions