asp.net 7 Razor Pages ‘Get’表单提交未生成预期的URL。

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

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 &quot;{handler?}/{myvar?}&quot;

&lt;form method=&quot;get&quot; asp-route-handler=&quot;search&quot;&gt;
	&lt;input name=&quot;myvar&quot; value=&quot;test&quot; &gt;
	&lt;button type=&quot;submit&quot;&gt;Search&lt;/button&gt;
&lt;/form&gt;

Cshtml.cs

public void OnGetSearch(string myvar)

Hi guys, from my understanding setting @page &quot;{handler?}/{myvar?}&quot; 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.

huangapple
  • 本文由 发表于 2023年4月20日 05:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059094.html
匿名

发表评论

匿名网友

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

确定