简体   繁体   中英

Handling Incoming Request URLs in ASP.NET

Currently trying to augment the search of an existing ASP.NET site. My background is Java, so if I was writing a server in Java, I could just handle the incoming request as a string and parse it however I wanted, taking out the search terms, etc.

What part of ASP handles that? Where should I be looking for where the incoming string is taken apart to handle the search request? There is a search button which redirects the page to a URL which includes the search parameters. That's where the trail goes cold for me as I need to know where it comes back into the server.

For example, once you've vetted the search term it gets submitted like this:

Response.Redirect("~/shop?" + type + "=" + searchBoxContent);

'type' is the type of search so that could be based on brand or searching within the product description, etc.

The site is already using some type of url rewriting as the url doesn't show up with any .aspx when you do a search. Should I be looking in a config file or a .master.cs file or where to point me in the right direction?

The easiest system for this is ASP.NET MVC which has a built-in Route and parameter handler.

See MSDN for docs.

Example:

{controller}/{action}/{id}

Can redirect to a Controller action:

public ActionResult Find(int id)
{ ... }

If this is not what you want, take a look at this blog article of Scott Guthrie on URL rewriting.

If you have a URL like this:

/shop.aspx?type=abc

then you can use Request.QueryString the get the value of type . This is the syntax:

Request.QueryString["type"]

For example if you want to get the value when /shop.aspx?type=abc is loaded at the first time, then you should add this code in Page_Load method inside the code behind ( shop.aspx.cs ):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // assign 'type' query string to typeOfSearch
        string typeOfSearch = Request.QueryString["type"];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM