英文:
asp.net 7 razor pages 'Get' form submit not producing expected url
问题
Goal/expectation: have a url that looks like: /search/test
Problem: I'm getting: /search?myvar=test
Cshtml
@page "{handler?}/{myvar?}"
<form method="get" asp-route-handler="search">
<input name="myvar" value="test" >
<button type="submit">Search</button>
</form>
Cshtml.cs
public void OnGetSearch(string myvar)
Hi guys, from my understanding setting @page "{handler?}/{myvar?}"
should have produced my expected url format.
Just in case it makes any difference, I also have a standard public void OnGet()
for accessing the page directly instead of via form submit. (The form is on the same page, and submits to itself but with the search
handler)
I've re-read the docs a couple of times but not seeing what I'm doing wrong.
Can someone suggest what I'm doing wrong?
Thanks
英文:
Goal/expectation: have a url that looks like: /search/test
Problem: I'm getting: /search?myvar=test
Cshtml
@page "{handler?}/{myvar?}"
<form method="get" asp-route-handler="search">
<input name="myvar" value="test" >
<button type="submit">Search</button>
</form>
Cshtml.cs
public void OnGetSearch(string myvar)
Hi guys, from my understanding setting @page "{handler?}/{myvar?}"
should have produced my expected url format.
Just in case it makes any difference, I also have a standard public void OnGet()
for accessing the page directly instead of via form submit. (The form is on the same page, and submits to itself but with the search
handler)
I've re-read the docs a couple of times but not seeing what I'm doing wrong.
Can someone suggest what I'm doing wrong?
Thanks
答案1
得分: 3
我认为你误解了@page
的作用。当你向@page
指令提供路由模板时,你指定了页面可以响应的URL模式。@page
不能限制表单生成的URL格式,它只能限制当前页面可以接受的URL格式。
根据规范(RFC1866,第46页;HTML 4.x 第17.13.3节)的规定:
如果方法是"get"且操作是HTTP URI,则用户代理将取操作的值,附加一个
?
,然后附加表单数据集,使用"application/x-www-form-urlencoded"内容类型进行编码。
当你使用"get"方法提交表单时,它会将所有值放入查询字符串而不是路由。所以你没有做错任何事情,只是误解了@page
的作用。
英文:
I think you misunderstand the role of @page
. When you supply a route template to the @page
directive, you specify a URL pattern that the page can respond to. @page
cannot limit the format of the url generated by the form, it can only limit the format of the url that the current page can accept.
As the specifications (RFC1866, page 46; HTML 4.x section 17.13.3) state:
> If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.
When you use the get
method to submit a form, it will put all the values into query string instead of route. So you didn't do anything wrong, you just misunderstand the role of @page
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论