为什么 Razor 页面不设置 GET 参数?

huangapple go评论68阅读模式
英文:

Why razor pages don`t set a get param?

问题

当我尝试使用 /?page=1 参数获取我的 Razor 页面时,我期望我的 page 参数将是1,但它仍然是0。为什么它没有从URL中读取,我可以怎么做?

public async Task OnGetAsync(string[] tags, int page)
{
    _logger.Log(LogLevel.Information, $"新的索引请求,页面 {page}");
    var find = new Api.FindApi(cs);
    posts = await find.Find(tags, Page * Constants.POST_PER_PAGE);
}

我还尝试了这个,但它仍然不起作用:

[FromQuery(Name = "page")]
public int Page { get; private set; }
英文:

When i try get my Razor page with /?page=1 params, i expect that my page param will be a 1, but it a still 0. Why is it not being read from the url and what can i do about it?

public async Task OnGetAsync(string[] tags, int page)
{
    _logger.Log(LogLevel.Information, $"New index request, page {page}");
    var find = new Api.FindApi(cs);
    posts = await find.Find(tags, Page*Constants.POST_PER_PAGE);
}

I also tried this, but it still not working

[FromQuery(Name = "page")]
public int Page{get;private set;}

答案1

得分: 1

使用 [FromQuery(Name ="page")] 在你的参数前面来解决这个问题,就像这样:

public async Task OnGetAsync([FromQuery(Name ="page")]int page, string[] tags)
{
    //Your Code
}
英文:

Use [FromQuery(Name ="page")] befor your parameter to solve this problem like this:

public async Task OnGetAsync([FromQuery(Name ="page")]int page, string[] tags)
{
    //Your Code
}

答案2

得分: 0

除了FromQuery属性之外,在Razor页面中,你可能应该在这些查询参数属性上使用BindProperty属性。

你还应该移除Page的setter上的private修饰符。

[BindProperty(SupportsGet = true)]
public int Page { get; set; }
英文:

Instead of the FromQuery attribute, in a Razor page you should probably be using the BindProperty attribute on those query param properties.

You should also remove the private modifier on the setter for Page

[BindProperty(SupportsGet = true)]
public int Page { get; set; }

huangapple
  • 本文由 发表于 2023年7月20日 17:47:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定