10

In Razor Pages, it is quite convenient that if you were to call, for example,

http://localhost/foo?bar=42

In the corresponding model, the bar key is automatically accessible in the OnGet constructor

public IActionResult OnGet(int bar)
{
    System.Console.WriteLine($"bar is {bar}");
}

But what if the query parameter is poo?

http://localhost/foo?poo=42

then in the model, bar does not get the value 42.

So simple enough, get the variables to match the query parameter key. But what if the key is hyphenated?

http://localhost/foo?foo-bar=42

foo-bar is definitely not an acceptable variable name. How do I access this query parameter? What are the rules here?

In my specific case, I don't really have a choice but to receive these hyphenated query string parameters. Also, I'm on .net core 2.2.

Dillon Chan
  • 145
  • 1
  • 2
  • 9

3 Answers3

24

I think heiphens are represented by underscores, so foo-bar becomes foo_bar, however that goes against the standard C# naming convention.

I wouldn't recommend binding query parameters as handler parameters anyway. The cleanest solution is to define a property on your PageModel like so:

// from the Microsoft.AspNetCore.Mvc namespace
[FromQuery(Name = "foo-bar")]
public string FooBar { get; set; }

This way, any time a query parameter matching that name is provided, it will always be bound. Regardless of whether or not the specific handler requested it. Then you can just access the property on your PageModel whenever you need it. So you example method becomes:

public void OnGet()
{
    System.Console.WriteLine($"bar is {FooBar}");
}
thomasrea0113
  • 360
  • 1
  • 9
  • Why is defining query parameters as fields in PageModel the cleanest solution? They are only required in methods/routes that need them, not the whole PageModel. It doesn't make sense to make them available in the whole class. – SulsDotK Jan 17 '23 at 19:57
  • I guess it's really more of a matter of opinion. Especially in more complex scenarios where you may have several query parameters and multiple page actions, it's less cumbersome to simply define your query parameters once on the PageModel and then reuse them where necessary. I don't see much of a point in trying to keep the handlers as more pure methods since you'll need to access the Request/Response class properties anyway. Might as well lean all the way into the class-based approach. – thomasrea0113 Jan 18 '23 at 20:24
9

The simplest solution in Razor Pages is to use Request.Query:

public void OnGet()
{
    var data = Request.Query["foo-bar"];
}
Mike Brind
  • 28,238
  • 6
  • 56
  • 88
-1

In a standalone/WebAssembly Razor Components, it can be achieved as follows

[Parameter]
[SupplyParameterFromQuery(Name = "foo")]
public string Foo { get; init; }

See here for more info.

JBSnorro
  • 6,048
  • 3
  • 41
  • 62
  • 3
    Take care, this is only for Razor components in Blazor not applicable to Razor Pages – Sven Jan 11 '23 at 10:49
  • tho the answer is technically wrong, in combination with the comment it highlights the difference for Blazor and RazorPages. – somedotnetguy Jan 13 '23 at 14:01