0

I have a few old sites that I want to add routing parameters. They were coded without using mvc so there are no global.asax with the handy MVC settings.

Currently I have a page with the url abc.com/xyz that has a search function. I can input a query which would send me to another page but it has the same url. I want to make it so that if i put some variation of the url abc.com/xyz?search='what_You_Query', it gives me the searched page. Right now that url sends me to the page where I input my query.

The website is coded in C# and html and saved in aspx files. The webpages also make use of jscripts

I'd appreciate any help I can get

Edit: Seems like there was some confusion, there is a search box that allows the user to query on the webpage. What I want is to allow users to directly link to a searched page.

John Wong
  • 31
  • 9
  • So you expect people to search by modifying the URL? Why not add search boxes to your master template (or each page) that route to a search page? Otherwise you're going to have to detect the `search` query string and either redirect or show different output (which seems messy) – D Stanley Jan 27 '16 at 20:13
  • The goal of this is to allow users to directly linked to a search page. Right now there is no way of accomplishing that. We can only hyperlink to the search page where you input the query but not the page with the actual results – John Wong Jan 27 '16 at 20:48
  • https://stackoverflow.com/questions/38161772/make-asp-net-mvc-route-id-parameter-required – Imtiyaz Moon Sep 14 '18 at 07:22

1 Answers1

1

You'd need to capture that on the page load - check the query string (https://msdn.microsoft.com/en-us/library/ms524784%28v=vs.90%29.aspx) and if search is in it, redirect to the search page.

MODIFIED TO INCLUDE MORE DETAILS

I'm assuming your working with web forms (Microsoft alternative to MVC). You would need to add a server-side (http://www.seguetech.com/blog/2013/05/01/client-side-server-side-code-difference) Page_Load event (https://msdn.microsoft.com/en-us/library/6w2tb12s.aspx). There the code would look something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if(Request.QueryString["search"] != null)
        Response.Redirect("/search?" + UrlEncode(Request.QueryString["search"]), true);
}

Please note I have not tested the code and am going by memory a bit here - but that should do the trick.

Brian Riley
  • 926
  • 1
  • 7
  • 12
  • Your suggestion might work. I just have a few questions to follow up on. Do you know If I input that line of code into the head tag of the aspx file or can I call it anywhere? Do you know how to place the query string(if detected) into a search box and automatically search? I'm sorry if these are very basic questions but I only have limited html and css experience. – John Wong Jan 27 '16 at 21:25
  • please see modified answer. – Brian Riley Jan 27 '16 at 23:49