开发者

Make URL in ASP.Net user-friendly

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 i开发者_C百科n 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.


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


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜