There are many ways to implement an url rewrite system in your web application.
This post shows the easiest way.
First, create a page called "default.aspx" with a Panel called "pnlMain".
In the page load event insert this code:
Code Snippet
- //Test the different page
- if (Request.QueryString["id"] == "1")
- pnlMain.Controls.Add(new LiteralControl("<b>Login</b>"));
- else if (Request.QueryString["id"] == "2")
- pnlMain.Controls.Add(new LiteralControl("<b>Content</b>"));
- else
- pnlMain.Controls.Add(new LiteralControl("<b>Home Page</b>"));
Add a global.asax file, create the Application_BeginRequest event and paste the follow code:
Code Snippet
- void Application_BeginRequest(object sender, EventArgs e)
- {
- string originalUrl = Request.Url.ToString();
-
- if (originalUrl.Contains("Login"))
- {
- Context.RewritePath("default.aspx?id=1");
- }
- else if (originalUrl.Contains("Content"))
- {
- Context.RewritePath("default.aspx?id=2");
- }
- else
- {
- Context.RewritePath("default.aspx?id=3");
- }
- }
Now you can launch your application in Debug mode. Add in the address bar the string "Login" and press Enter. You can try with another value "Content".
This example is very simple but it is a good way to understand how url rewrite class works.
There are more ways to get the console output text from C#.
In this example I created a function with two parameters.
The first called "cmdPath" is the path of the process to launch.
The second called "arguments" are the arguments of the process.
Code Snippet
- void RunWithRedirect(string cmdPath,string arguments)
- {
- var proc = new Process();
- proc.StartInfo.FileName = cmdPath;
- proc.StartInfo.Arguments = arguments;
-
- // set up output redirection
- proc.StartInfo.RedirectStandardInput = true;
- proc.StartInfo.RedirectStandardOutput = true;
- proc.StartInfo.RedirectStandardError = true;
-
- proc.StartInfo.UseShellExecute = false;
-
- proc.EnableRaisingEvents = true;
- proc.StartInfo.CreateNoWindow = true;
-
- proc.Start();
-
- StreamReader reader = proc.StandardOutput;
- string s = reader.ReadToEnd();
- txtConsole.Text += s;
-
- proc.WaitForExit();
- }
This HOW TO describes one of the various built-in methods .NET provides to use XML returned by a web service.
The simplest way to view the returned data is to get the response stream and put it into a string. This is especially handy for debugging. The following code gets a web page and put the the contents in a string.
public static string GetPageAsString(string address)
{
string result = string.Empty;
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Get the web response
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
// Get the stream from response
StreamReader reader = new StreamReader(response.GetResponseStream());
// Read the whole contents and return as a string
result = reader.ReadToEnd();
reader.Dispose();
reader = null;
request = null;
return result;
}
To call this method write this code:
GetPageAsString("http://weather.yahooapis.com/forecastrss?p=ITXX0042&u=f");
Here there is the complete sourcecode.
GetXmlFromWebIntoString.rar (37.00 kb)
This evening I created a project in C# that explains how to remove html tags from string with regular expressions.
I created a function to semplify this operation.
private string removeHmtlFromString(string strInput)
{
try
{
Regex objRegExp = new Regex("<(.|\n)+?>");
return objRegExp.Replace(strInput, String.Empty).Trim();
}
catch
{
return string.Empty;
}
}
You can download the full work project from this article.
RemoveHtmlFromString.rar (36.17 kb)
When you got a string array and you want turn into a string, you have two ways.
This way is useful if you work with every single string before convert.
string[] strings = { "hello", "world", "I", "am", "an", "array" };
string result = "";
foreach(string s in strings) {
result += s + " ";
}
This way is the best for performance and for best code.
string output = string.Join(" ", strings);
That's all folks!!!
StopWatch class can measure elapsed time for one interval.
We can use for measuring performance of the new code blocks or algorithms. Use IsRunning method to determine the state of StopWatch object.
Use Start method to begin measuring elapsed time, and Stop method to stop measuring elapsed time.
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
textBox1.Text = DateTime.Now.TimeOfDay.ToString();
Application.DoEvents();
// Perform a long process
Thread.Sleep(34564);
stopwatch.Stop();
textBox2.Text = DateTime.Now.TimeOfDay.ToString();
textBox3.Text = stopwatch.Elapsed.Milliseconds.ToString();
Application.DoEvents();
}