Asp.net UrlPathEncode

Asp.net UrlPathEncode

Where to use UrlPathEncode? In Asp.net you can encode the URL to avoid breaking of the request. Here we will see how to do web page path encoding.

HttpUtility.UrlEncode("My Path") returns out put as My+Path. You can see it replaces the space to +. However, HttpUtility.UrlPathEncode("My Path") returns My%20Path. Here space gets converted into %20.

If you are having space or nay other special character in web page path. Don't forget to use UrlPathEncode.

Asp.net UrlPathEncode Example.
using System;
using System.Web;


public partial class URLEncodeDemo : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string filepath = "Monthly Reports 01 12 2012.csv";

    string url = "http://www.satalaj.com/" + HttpUtility.UrlPathEncode(filepath);

    Response.Redirect(url);

  }
}
VB.net UrlPathEncode example.
Imports System
Imports System.Web


Public Partial Class URLEncodeDemo
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(sender As Object, e As EventArgs)
		Dim filepath As String = "Monthly Reports 01 12 2012.csv"

		Dim url As String = "http://www.satalaj.com/" + HttpUtility.UrlPathEncode(filepath)

		Response.Redirect(url)

	End Sub
End Class

Tags:

Asp.net Interview Questions for 2 years or 3 years experianceasp net image uploadASp.net 2.0 step by step Membership Provider ASP.net File HandlingWhat is web garden in Asp.net?Asp.net open new windowAsp.net Convert String To ImageAsp.net Convert Image To Base64Hack asp.netListBox Asp.net Binding csharpAsp.net How To Write a Cookie and Read

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.