使ASP.Net中的URL用户友好

时间:2010-01-05 02:41:54

标签: asp.net routing asp.net-3.5 friendly-url asp.net-4.0

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

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

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

如果他的输入是sfs中的单词TextBox1。 所以我需要回复他的回复。我想在用户友好的网址上显示此回复,例如

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框架中出现的RouteCollection.MapPageRoute来解决,但我不清楚如何使用它。

感谢任何帮助。

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

UPDATE2。表单必须是服务器控件,它有除提交和文本框之外的其他控件。这将是好的(但是,如果这些参数没有出现在浏览器中显示的URL中,则仍然没有必要。

3 个答案:

答案 0 :(得分: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")。

答案 1 :(得分:0)

嗯,正在使这个'看起来很糟糕'的主要原因是你正在使用ViewSate和GET;所以不要这样做(要么禁用ViewSate并相应地调整代码,要么使用POST)。

然而,您可能也感兴趣的是URL重写。您可以通过几种方式实现这一点,我通常在IIS中使用通配符映射并对Global.asax文件进行适当的更改。搜索将揭示如何执行此操作。

答案 2 :(得分:0)

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

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

然后在ASP.NET端,您可以使用URL Rewriting功能将该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>
相关问题