2

我正在尝试使用 Web 窗体在 ASP.Net 中开发我的第一个站点。

我有一个带有一些控件和一个 TextBox 控件的表单。而现在我使用GET请求。当用户提交表单时,他的浏览器期望获得长 URL,例如

http://mysite.com/search.aspx?__VIEWSTATE=%2FwEPDwUJNTE2NjY5jMY4D2QWAgICD2QWAgIDDW8wAh4EVGV4dAUBMWRkZKthQ0zeIP5by49qIHwSuW6nOj8iLTdoCUzpH369xyg8&__EVENTVALIDATION=%2FwEWAwLnrcHhBQLs0bLrBgKM54rGBjGtX5fJOylLy4qRbt6DqPxO%2FnfcMOkHJBRFqZTZdsBD&TextBox1=sfs&Button1=Button

如果他的输入sfsTextBox1. 所以我需要回复他。我想在用户友好的 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。

在我看来,可以通过RouteCollection.MapPageRoute4.0 框架中出现的方法来解决,但我不清楚如何使用它。

任何帮助表示赞赏。

更新。使用POST代替 不是问题GET。但是这样 URL 总是看起来像http://mysite.com/search.aspx

更新2。表单必须是服务器控件,并且除了提交和文本框之外,它还有其他控件。这会很好(不过,如果此参数没有出现在浏览器中显示的 URL 中,则仍然没有必要。

4

3 回答 3

1

不幸的是,对 ASP.NET 服务器表单使用 GET 请求总是会产生那些“丑陋”的 URL。

您可以做的一件事是将表单更改为不是服务器表单而是常规表单:

<form method="get" action="Search.aspx">
    <input type="text" name="query" />
    <input type="submit" name="SearchButton" value="Search" />
</form>

此解决方案的一个限制是您不能再将某些 ASP.NET 控件放置在此表单中。例如,该<asp:Button>控件将无法在此窗体中工作,因为它必须包含在服务器窗体(即其上具有的窗体runat="server")中。

于 2010-01-05T02:44:53.137 回答
0

好吧,让“看起来很糟糕”的主要原因是您使用的是 ViewSate 和 GET;所以不要那样做(要么禁用 ViewSate 并相应地调整代码,要么使用 POST)。

但是,您可能还感兴趣的是 URL 重写。您可以通过几种方式做到这一点,我通常使用 IIS 中的通配符映射和对Global.asax文件的适当更改来做到这一点。搜索将揭示如何做到这一点。

于 2010-01-05T02:44:49.453 回答
0

由于它是一个 GET 请求,因此您也可以使用 javascript,设置

location.href = 'http://mysite.com/search/' + query; 

然后在 ASP.NET 端,您可以使用URL 重写功能将该 URL 重定向到特定的 ASPX 页面作为查询字符串参数。

如果您想要更详细的样品,请告诉我。

样本:

这是一个示例,请注意我尚未对其进行测试,但这应该可以帮助您入门。

<html>
<head>
  <script type="text/javascript">
    function searchRedirect()
    {
      var query = $get('query');
      location.href = "/search/" + query.value;
    }
  </script>
</head>
<body>
    <div class="search">
        <input type="text" id="query" /><br />
        <input type="button" id="search" value="Search" onclick="searchRedirect();" />
    </div>
</body>
</html>

然后在重定向端,你有一个这样的 RouteModule:

public class UrlRewriter : IHttpModule
{
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.AuthorizeRequest += new EventHandler(OnBeginRequest); //this ensures the login page has the vitual url not the mapped url
    }


    private void OnBeginRequest(object sender, EventArgs e)
    {
        var application = sender as HttpApplication;
        if (application != null)
        {
            var requestPath = application.Request.AppRelativeCurrentExecutionFilePath;
            if (requestPath.ToLower().StartsWith("/search/"))
            {
                var query = requestPath.Substring(8);
                application.Context.RewritePath("Search.aspx", null, "query=" + query, false);
            }
            // .. Other Routes
        }
    }
}

假设代码在您的 App_Code 文件夹中,您可以在 web.config 中使用它

<system.web>
  <!-- ... -->
  <httpModules>
      <add name="UrlRewriter" type="UrlRewriter, __code"/>
  </httpModules>
</system.web>

<!-- If IIS7 -->
<system.webServer>
  <modules>
    <add name="UrlRewriter" type="UrlRewriter, __code" />
  </modules>
</system.webServer>
于 2010-01-05T02:51:47.257 回答