Skip to main content

Get website's base URL..



Following function is help you to get root path or site base URL of your web application.
Like if your page URL like "http://www.blogger.com/post-create.g?blogID=5406317769839137062"
this function return base URL like "http://www.blogger.com".

public static string GetApplicationPath()
{
     string appPath = null;

     //Getting the current context of HTTP request
     HttpContext context = HttpContext.Current;

     //Checking the current context content
     if (context != null)
     {
                //Formatting the fully qualified website url/name
                appPath = string.Format("{0}://{1}{2}{3}",
                context.Request.Url.Scheme,
                context.Request.Url.Host,
                context.Request.Url.Port == 80 ? string.Empty : ":" + context.Request.Url.Port,
                context.Request.ApplicationPath);
     }
     return appPath;
}

Better One

public static string GetApplicationPath()
{

var request = HttpContext.Current.Request;
var appUrl = HttpRuntime.AppDomainAppVirtualPath;

if (!string.IsNullOrWhiteSpace(appUrl) && appUrl != "/")
    appUrl += "/";

var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);

 return baseUrl;

}

Popular posts from this blog

LINQ union with group by

In my recent development activity with LINQ query, I need to get the some of user points from tow different tables where user's points are based on some activities. There are many users who are into one table but not in another table and some are in both table. For example tables having data like this Table 1 UserID   Points 1           10 2           20 Table 2 UserID    Points 2           20 3           30 Result should be UserID    Points 1           10 2           40 3           30 I try to get this result with LINQ query using joins and group by samples from the web. I had googling to get something useful but dont get any success. So I try with new logic and here it is, I think it will help full to other also. Example Item class to use public class Item {   public int ID { get ; set ; }   public int Total { get ; set ; }   public Item()   {   }   public Item( int ID, int Total)   {     this .ID = ID; this .Total = Total;   }

SqlConnection from ObjectContext.Connection

Following is C# code to get the  SqlConnection from ObjectContext.Connection . This is usefully to run dynamically created query using ADO.NET using connection string specified into the EDMX ObjectContext object. C# Code Example : using ( EntityContext context = new EntityContext ()) { EntityConnection ec = ( EntityConnection )context.Connection; SqlConnection sc = ( SqlConnection )ec.StoreConnection; SqlDataAdapter da = new SqlDataAdapter (strQuery, sc); DataSet ds = new DataSet (); da.Fill(ds); return ds.Tables[0]; }

Get Tables and Columns Names by LINQ

Here I am explaining one of the tricks to get all table name list along with column of it from your DataContext. I am using the System.Data.Linq.Mapping.MetaModel to get information about tables and it have much more database related information. Following are the code to list all table name along with column from your DataContext. //DataContext object. DataClassesDataContext DB = new DataClassesDataContext ();         //String variable to stor html to render. string strHtmlToLoad = "" ;         //Heading strHtmlToLoad = "<-h2->Table Names<-/h2-><-ol->" ;         //Loop through tables add name to string foreach ( var mTable in DB.Mapping.GetTables()) { strHtmlToLoad += "<-li->" + mTable.TableName + "<-ul->" ; //Loop through table's columns add name to string foreach ( var mColumn in mTable.RowType.DataMembers) strHtmlToLoad += "<-li->