1
votes

I have a razor page call "app" in the default Pages folder.

I can view this page just fine with http://myapp.com/app

Now I want to add a route parameter so the url looks like this: http:/myapp.com/app/greg

I want to extract greg from the URL and use it in the page.

So I tried this:

HttpGet("app/{name}")]
public void OnGet(string name)
{
    //Do something with name.
}

But I get a 404 error.

1

1 Answers

1
votes

It seems attribute routing is not supported in Razor Pages. This ended up working for me.

app.cshtml:

@page "{name}"

app.cshtml.cs:

[BindProperty(SupportsGet = true)]
public string Name{ get; set; }

public void OnGet()
{
     //Do something with this.Name...
}

The page directive creates the route and the BindProperty attribute binds the value to the property.

If you want to get the value from the query string instead, change @page "{name}" to just @page