使 ASP.Net 中的 URL 对用户友好

发布于 2024-08-16 15:54:41 字数 1108 浏览 10 评论 0原文

我正在尝试使用 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 GETrequest. 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

or
http://mysite.com/sfs

or
http://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕巷 2024-08-23 15:54:41

不幸的是,使用 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 控件。例如, 控件在此表单中不起作用,因为它必须包含在服务器表单中(即具有 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:

<form method="get" action="Search.aspx">
    <input type="text" name="query" />
    <input type="submit" name="SearchButton" value="Search" />
</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 has runat="server" on it).

倾听心声的旋律 2024-08-23 15:54:41

由于它是 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>

Since its a GET request you can also use javascript, setting the

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

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.

<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>

Then on the redirect side you have have a RouteModule like this:

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
        }
    }
}

And assuming the code is in your App_Code folder you could use this in your 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>
舂唻埖巳落 2024-08-23 15:54:41

好吧,导致“看起来很糟糕”的主要原因是您正在使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文