英文:
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; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论