Simple way to implement Url Rewrite Class

By Emanuele Bartolesi at April 05, 2010 17:50
Filed Under: Asp.net, C#

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
  1. //Test the different page
  2. if (Request.QueryString["id"] == "1")
  3.     pnlMain.Controls.Add(new LiteralControl("<b>Login</b>"));
  4. else if (Request.QueryString["id"] == "2")
  5.     pnlMain.Controls.Add(new LiteralControl("<b>Content</b>"));
  6. else
  7.     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
  1. void Application_BeginRequest(object sender, EventArgs e)
  2. {
  3.     string originalUrl = Request.Url.ToString();
  4.  
  5.     if (originalUrl.Contains("Login"))
  6.     {
  7.         Context.RewritePath("default.aspx?id=1");
  8.     }
  9.     else if (originalUrl.Contains("Content"))
  10.     {
  11.         Context.RewritePath("default.aspx?id=2");
  12.     }
  13.     else
  14.     {
  15.         Context.RewritePath("default.aspx?id=3");
  16.     }
  17. }

 

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.

 

How to get console output text from C#

By Emanuele Bartolesi at March 28, 2010 16:56
Filed Under: C#

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
  1. void RunWithRedirect(string cmdPath,string arguments)
  2. {
  3. var proc = new Process();
  4. proc.StartInfo.FileName = cmdPath;
  5. proc.StartInfo.Arguments = arguments;
  6.  
  7. // set up output redirection
  8. proc.StartInfo.RedirectStandardInput = true;
  9. proc.StartInfo.RedirectStandardOutput = true;
  10. proc.StartInfo.RedirectStandardError = true;
  11. proc.StartInfo.UseShellExecute = false;
  12.  
  13. proc.EnableRaisingEvents = true;
  14. proc.StartInfo.CreateNoWindow = true;
  15. proc.Start();
  16.  
  17. StreamReader reader = proc.StandardOutput;
  18. string s = reader.ReadToEnd();
  19. txtConsole.Text += s;
  20.  
  21. proc.WaitForExit();
  22. }

 

How to get string from Http Request

By Emanuele Bartolesi at February 24, 2010 18:05
Filed Under: Asp.net, C#, WebServices

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)

How to remove html tags from string with Regular Expressions

By Emanuele Bartolesi at October 27, 2009 15:53
Filed Under: C#

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)

How to convert string array to string in C#

By Emanuele Bartolesi at September 08, 2009 04:56
Filed Under: C#

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!!! Wink

How to measure elapsed time in C# with StopWatch

By Emanuele Bartolesi at March 20, 2009 04:50
Filed Under: Asp.net, C#

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();
}

About me

I will also give you some useful tips, based on the modest wisdom gained during the years that I've worked as a developer and project manager.

Widget

Ohloh profile for Emanuele Bartolesi

 

Wakoopa

Software tracking

 

Software tracking

from Amazon



hacker emblem



Scarica il pdf di Game
Rivista Game di videogiochi
Trucchi videogiochi