2

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.

flashnik
  • 1,900
  • 4
  • 19
  • 38

3 Answers3

1

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

Eilon
  • 25,582
  • 3
  • 84
  • 102
0

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.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • Even if you disable ViewState there will still be at least a small __VIEWSTATE field (and possibly others). I guess it's a matter of whether the question is about how to make the URL completely "pretty" or just "less ugly" :) – Eilon Jan 05 '10 at 02:46
  • I updated my question. I can use `POST` but in this case it will be constant and not pretty as much as I want - I want to have separate URL for each word entered in TextBox. Something like Google does - for every search request it has own url and serves it without temporary redirects. – flashnik Jan 05 '10 at 02:52
  • Well, in that case you want URL re-writing. Search around for it; you will find some tutorials on it very easily. – Noon Silk Jan 05 '10 at 02:52
  • How will it work? I mean how can I form beutiful URL in browser wothout redirect? AFAIK rewriting will make create long URL from beautiful but how to set this smart? – flashnik Jan 05 '10 at 03:08
0

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>
bendewey
  • 39,709
  • 13
  • 100
  • 125
  • Yes, I'm very interesting in your solution. Could you provide more detailed sample? – flashnik Jan 05 '10 at 02:57
  • This solution of course depends on the user having JavaScript enabled. Do you need this app to work on all browsers, including mobile phones, as well as users who use accessibility aids such as screen readers? – Eilon Jan 05 '10 at 02:59
  • I assume that JS is enabled because It'll also use AJAX And first of all I aim on desktop browsers - IE, Firefox, Safari, Opera – flashnik Jan 05 '10 at 03:02
  • @flashnik, what are you using for you AJAX? jQuery? ASP.NET AJAX? – bendewey Jan 05 '10 at 03:16
  • Yes, I've seen and supported. I was checking one of my ideas. Great thanks for your solution! It does what I want. And which limitations I may encounter with such code? I mean why I was asked about browsers and phones? – flashnik Jan 05 '10 at 04:05
  • The limitation mentioned is regarding browsers without javascript enabled or javascript support. – bendewey Jan 05 '10 at 12:48