Adding search parameter to url to enable direct search from address bar - c#

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.

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.

Related

Parsing HTML in C# that is updating constantly

I have a webpage that is displaying some data using AJAX queries. I would need to parse some of this data in a C# program.
Problem is that when I look at the source code of my webpage, this is not showing up the data, as this is being generated automatically by an AJAX script and modifying the DOM.
If I select everything on the webpage and do "Inspect Element" with Chrome, I have the full HTML code with the data I want to extract that are in various tables.
What I've tried is doing a webBrowser1.Navigate("www.site.com"), and then in my webBrowser1_DocumentCompleted() event, I'm doing this:
var name = webBrowser1.Document.GetElementById("table_1_r_7_c_2");
Problem is that webBrowser1 is not returning the full HTML code, as some code is generated by the AJAX queries.
Does anyone know how I could achieve this behavior in C#?
The DocumentCompleted event is a bit misleading because it will also fire for each AJAX request on the page. You can do something like this to check if it's the actual page that's loaded, or some other variant to look for specific requests.
private void OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.AbsolutePath == webBrowser1.Url.AbsolutePath)
{
// page loaded
}
}

how to call one webpage from another webpage in asp.net

I have 2 web applications. webapp1 is running at location say - weblocationlocation1/webapp1/default.aspx
and webapp2 is running at different location say -
weblocationlocation2/webapp2/default.aspx
Now, If I want to call webapp2/default.aspx from webapp1 then how to call.
how to run Page_Load(object sender, EventArgs e) of webapp1 from webapp2/default.aspx.
I have to stay on webapp1/default.aspx in my browser. and still want to load webapp2/default.aspx (ONLY from my code of button clicked). in this case, how to store cookie/session variables. and want to maintain them in webapp1 across all pages.
If you want to do this via a redirect then:
Response.Redirect("weblocationlocation2/webapp2/default.aspx");
Or directly on the server use
Server.Transfer("weblocationlocation2/webapp2/default.aspx");
Or
Server.Execute("weblocationlocation2/webapp2/default.aspx");
The last will return control to the calling method (the second won't).
as described by # Justin Harvey you can use Page_load() method and call Response.redirect method to redirect to your desired web page
You can also use javascript if you want to redirect to your page on event such as button on click
for that you can do following
btn_demo_onClick()
{
window.location = "abc.aspx";
}
it just a complementary option if you want to go with javascript
Thanks
Response.Redirect("default.aspx"); // At URL You will Get the default page as what you are redirecting to.
Server.Transfer("default.aspx"); // At URL You will not Get the default page as what you are redirecting to.
example : If you are logged in Login page then you want to redirect to default page ,then you can use both the above mentioned methods.

Dynamic Pages From a Database in C#

Forgive me if this has already been asked somewhere, but I cannot figure out the best way to accomplish this task. I want to be able to create a rendering system that will allow me to render out content from thousands of different .aspx pages without having to create thousands of .aspx pages. That being said, I still want to be able to render out the appropriate .aspx page if it exists in my code.
For example, when a request is made to the site, I want to check and see if that URL is in the database, if it is, then I want to render the content appropriately. However, if it doesn't, then I want it to continue on to rendering the real .aspx page.
In trying to use an HTTPModule, I cannot get the page that exists in the database to write out the appropriate content. Here's my code.
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
Uri url = application.Context.Request.Url;
//Checks to see if the page exists in the database
PageInformation page = PageMethods.GetPageFromUrl(url.AbsolutePath);
if (page != null)
{
string renderedPage = Renderer.RenderPage(page);
application.Context.Response.Write(renderedPage);
}
}
However, when trying to use an HTTPHandler, I can't get the real .aspx pages to render appropriately because the *.aspx verb is being dealt with by the handler.
If anyone has any better ideas on how to completely re-design this, I'm completely open to that as well. Thanks.
This will do the trick:
Type page_type = BuildManager.GetCompiledType ("~/page.aspx");
Page page = (Page) Activator.CreateInstance (page_type);
page.ProcessRequest (Context);
I think you're lookign for a simple URL rewriting example.
So you have a single page "default.aspx" that could take an argument of the content you want to display "default.aspx?page=home", but you don't want the nasty query string part "?page=home".
this is best solved by URL rewriting which can be used as an ISAPI module in IIS. So instead of the URL string above, people see a page called "home.aspx", and the web server translates this into "default.aspx?page=home" for your page which can go get the content for the "home" page out of the DB and display it on the screen.
Here's a page with more information on a good implementation of this process:
http://www.opcode.co.uk/components/rewrite.asp
I believe this shows how to process the "normal" pages inside a handler
other example

How do I HTTP POST from an ASP.NET button?

Sorry, another super basic ASP.NET question. this so embarrassing.
I am reading the article on How to: Pass values between ASP.NET pages
In the second approach, they suggest hooking up a button and directing the user to another page using POST. I don't know how to do this. How do I HTTP POST?
"When the source page uses the HTTP POST action to navigate to the target page, you can retrieve posted values from the Form collection in the target page."
This is how I am sending the user to the new page:
protected void btnSubmitForPost_Click(object sender, EventArgs e)
{
Response.Redirect("GetViaPost.aspx");
}
EDIT
The final solution:
You can use ASP.NET webforms. Do the following: On the first page, create your controls and a button that sends the user to a new page. Handle the click event from this button. As stated below, use Server.Transfer with endResponse=false, instead of Response.Redirect(). When you use Response.Redirect, your post data is cleared out. I did not need to specify action in the form or anything else.
In ASP.NET when you click a button, you're posting the entire page's fields by default (as it's contained within a gigantic <form /> tag last time I checked. You can access these values after clicking the button like this:
string MyPostedValue = Request.Form["MyFormField"];
*Edit as per your update in your question, change Response.Redirect() to Server.Transfer() like this:
protected void btnSubmitForPost_Click(object sender, EventArgs e)
{
Server.Transfer("GetViaPost.aspx", true);
}
Then in your GetViaPost.aspx's page you can get any form/query string variable you passed from your sending page like this:
string MyPostedValue = Request.Form["MyFormField"];
If I'm reading this right, all of these answers are missing the question...
You're looking at posting from one Asp.Net form to another, and one of the methods is what you want to figure out - doing a normal http post. The book or article probably is already telling you about the Server.Transfer as another option if I'm guessing right.
If I'm getting the question right, then the simplest answer is to not use a standard ASP.Net form (with the runat = server attribute) as the starting point, but to use a simple standard html form to post to an asp.net page
<form action = "targetpage.aspx" method="post">
...some form fields here
<input type = "submit">
</form>
If in the codebehind you wire up to the button click event, then click the button. It's a POSTback that happens.
Any controls that you have runat="server" will be accessible by their id (and any values set on them) in the codebehind.
In terms of posting data to other pages, you have a number of options available to you.
The querystring, sessions, cookies and viewstate.
A basic example (with no error handling) given your updated Response.Redirect might be:
int someId = int.Parse(txtBoxOnThePage.Text);
Response.Redirect(string.Format("GetViaPost.aspx?myId={0}", someId));
Then on the GetViaPost page you could pull that out by:
HttpContext.Current.Request.QueryString["myId"]
http://www.asp.net/learn/ is a surprisingly good source of information and tutorials for this kind of learning.
ASP.NET buttons always perform a POST. You can set which page the button posts to using the PostBackUrl property of the button. If you leave this blank, the button will post back to the same page that is resides on.
Check out this article for more information.

URL and Query management Asp.Net C#

Ok so while back I asked question Beginner ASP.net question handling url link
I wanted to handle case like this www.blah.com/blah.aspx?day=12&flow=true
I got my answer string r_flag = Request.QueryString["day"];
Then what I did is placed a code in Page_Load()
that basically takes these parameters and if they are not NULL, meaning that they were part of URL.
I filter results based on these parameters.
It works GREAT, happy times.... Except it does not work anymore once you try to go to the link using some other filter.
I have drop down box that allows you to select filters.
I have a button that once clicked should update these selections.
The problem is that Page_Load is called prior to Button_Clicked function and therefore I stay on the same page.
Any ideas how to handle this case.
Once again in case above was confusing.
So I can control behavior of my website by using URL, which I parse in Page_Load()
and using controls that are on the page.
If there is no query in URL it works great (controls) if there is it overrides controls.
Essentially I am trying to find a way how to ignore parsing of url when requests comes from clicking Generate button on the page.
Maybe you can put your querystring parsing code into IsPostBack control if Generate button is the control that only postbacks at your page.
if (!IsPostBack)
{
string r_flag = Request.QueryString["day"];
}
As an alternative way, at client side you can set a hidden field whenever user clicks the Generate button, then you can get it's value to determine if the user clicked the Generate button and then put your logic there.

Categories

Resources