使 ASP.Net 中的 URL 对用户友好
我正在尝试使用 Web 窗体在 ASP.Net 中开发我的第一个网站。
我有一个带有一些控件和一个 TextBox 控件的表单。现在我使用 GET
请求。当用户提交表单时,他的浏览器期望获得长 URL,例如
http://mysite.com/search.aspx?__VIEWSTATE=%2FwEPDwUJNTE2NjY5jMY4D2QWAgICD2QWAgIDDW8wAh4EVGV4dAUBMWRkZKthQ0zeIP5by49qIHwSuW6nOj8iLTdoCUzpH369xyg8&__EVENTVALIDATION=%2FwEWAwLnrcHhBQLs0bLrBgKM54rGBjGtX5fJOylLy4qRbt6DqPxO%2FnfcMOkHJBRFqZTZdsBD&TextBox1=sfs&Button1=Button
他的输入是 TextBox1
中的单词 sfs
。 所以我需要回复他。我想在用户友好的 URL 上显示此响应,例如
http://mysite.com/search.aspx?TextBox1=sfs
或 http://mysite.com/sfs
或 http://mysite.com/search/sfs
我该怎么做?如果我使用 Response.Redirect,它首先返回 302,然后才处理短 URL。 Server.Transfer 不会更改 URL,用户会在浏览器中看到丑陋的长 URL。
在我看来,可以通过 4.0 Framework 中出现的 RouteCollection.MapPageRoute
来解决,但我不清楚如何使用它。
任何帮助表示赞赏。
更新。使用POST
代替GET
没有问题。但这样 URL 将始终看起来像 http://mysite.com/search.aspx
UPDATE2。表单必须是服务器控件,并且它还有除提交和文本框之外的其他控件。这会很好(但是,如果此参数没有出现在浏览器中显示的 URL 中,则仍然没有必要。
I'm trying to develop my first site in ASP.Net using Web Forms.
I have a form with some controls and a TextBox control. While now I use GET
request. When user submits a form his browser expects to get long URL, something like
http://mysite.com/search.aspx?__VIEWSTATE=%2FwEPDwUJNTE2NjY5jMY4D2QWAgICD2QWAgIDDW8wAh4EVGV4dAUBMWRkZKthQ0zeIP5by49qIHwSuW6nOj8iLTdoCUzpH369xyg8&__EVENTVALIDATION=%2FwEWAwLnrcHhBQLs0bLrBgKM54rGBjGtX5fJOylLy4qRbt6DqPxO%2FnfcMOkHJBRFqZTZdsBD&TextBox1=sfs&Button1=Button
if his input is a word sfs
in TextBox1
.
So I need to return him response. I would like to show this response on a user-friendly URL like
http://mysite.com/search.aspx?TextBox1=sfs
orhttp://mysite.com/sfs
orhttp://mysite.com/search/sfs
How can I do that? If I use Response.Redirect, it first returns 302, and only then work on short URL. Server.Transfer doesn't change URL and user sees ugly long URL in browser.
It seems to me that it is possible to solve via RouteCollection.MapPageRoute
which appeared in 4.0 Framework but it's unclear to me how I can use it.
Any help is appreciated.
UPDATE. It is not a problem to use POST
instead of GET
. But in this way URL will always look like http://mysite.com/search.aspx
UPDATE2. The form MUST be server control and it has another controls except submit and textbox. It would be good (though, still, not necessary if this parameters don't appear in URL showing in the browser.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,使用 ASP.NET 服务器表单的 GET 请求总是会产生那些“丑陋”的 URL。
您可以做的一件事是将表单更改为常规表单,而不是服务器表单:
此解决方案的一个限制是您不能再在此表单中放置某些 ASP.NET 控件。例如,
控件在此表单中不起作用,因为它必须包含在服务器表单中(即具有runat="server"< /code> 就可以了)。
Using GET requests with ASP.NET server forms will unfortunately always yield those "ugly" URLs.
One thing that you can do is change the form to not be a server form and instead be a regular form:
One limitation of this solution is that you can no longer place certain ASP.NET controls within this form. For example, the
<asp:Button>
control will not work in this form because it must be contained within a server form (that is, a form that hasrunat="server"
on it).由于它是 GET 请求,您还可以使用 javascript,设置
然后在 ASP.NET 端您可以使用 URL 重写 功能可将该 url 作为查询字符串参数重定向到特定的 ASPX 页面。
如果您想要更详细的示例,请告诉我。
示例:
这是一个示例,请注意我尚未对其进行测试,但这应该可以帮助您入门。
然后在重定向方面,您有一个像这样的 RouteModule:
假设代码位于您的 App_Code 文件夹中,您可以在 web.config 中使用它
Since its a GET request you can also use javascript, setting the
Then on the ASP.NET side you can use the URL Rewriting feature to redirect that url to a specific ASPX page as a query string parameter.
Let me know if you would like a more detailed sample.
Sample:
Here is a sample, please note I haven't tested it, but this should get you started.
Then on the redirect side you have have a RouteModule like this:
And assuming the code is in your App_Code folder you could use this in your web.config
好吧,导致“看起来很糟糕”的主要原因是您正在使用 ViewSate 和 GET;所以不要这样做(要么禁用 ViewSate 并相应地调整代码,要么使用 POST)。
不过,您可能还对 URL 重写感兴趣。您可以通过多种方式来完成此操作,我通常通过 IIS 中的通配符映射并对
Global.asax
文件进行适当更改来实现。搜索将揭示如何执行此操作。Well, the main thing that's making that 'look bad', is you're using ViewSate and GET; so don't do that (either disable the ViewSate and adjust code accordingly, or use POST).
What you may also be interested in, however, is URL Re-Writing. You can do that in a few ways, I typically do it with a wildcard mapping in IIS and appropriate changes to the
Global.asax
file. Searching will reveal how to do this.