Add existing folder into Visual Studio (The easy way)

5. March 2010

Ack!

Have you ever selected your folder and said "Exclude from Project" and then if you wanted to add that folder back into your solution, you couldn't. Using "Add existing item" would show you the file dialog and when you select your folder it drills down into that folder. GRR!.

 

 

Well there is a better way: 

In your solution explorer window if you select the icon "Show all Files" 

and then "Right click" your hidden folder and select "Include in Project"

Be certain the folder is in the correct file location within your folder structure to see it.

TechArticle

ASP.NET Paths reference (Content Repost)

1. March 2010

<Taken from Rick Strahl's Web log> at: http://www.west-wind.com/weblog/posts/132081.aspx

ASP.Net includes quite a plethora of properties to retrieve path information about the current request, control and application. There's a ton of information available about paths on the Request object, some of it appearing to overlap and some of it buried several levels down, and it can be confusing to find just the right path that you are looking for.

To keep things straight I thought it a good idea to summarize the path options along with descriptions and example paths. I wrote a post about this a long time ago in 2004 and I find myself frequently going back to that page to quickly figure out which path I’m looking for in processing the current URL. Apparently a lot of people must be doing the same, because the original post is the second most visited even to this date on this blog to the tune of nearly 500 hits per day. So, I decided to update and expand a bit on the original post with a little more information and clarification based on the original comments.

Request Object Paths Available

Here's a list of the Path related properties on the Request object (and the Page object). Assume a path like http://www.west-wind.com/webstore/admin/paths.aspx for the paths below where webstore is the name of the virtual.

Request Property Description and Value
ApplicationPath Returns the web root-relative logical path to the virtual root of this app. 
/webstore/
PhysicalApplicationPath Returns local file system path of the virtual root for this app. 
c:\inetpub\wwwroot\webstore
PhysicalPath Returns the local file system path to the current script or path
c:\inetpub\wwwroot\webstore\admin\paths.aspx
Path 
FilePath 
CurrentExecutionFilePath 
All of these return the full root relative logical path to the script page including path and scriptname. CurrentExcecutionFilePath will return the ‘current’ request path after a Transfer/Execute call while FilePath will always return the original request’s path. 
/webstore/admin/paths.aspx
AppRelativeCurrentExecutionFilePath Returns an ASP.NET root relative virtual path to the script or path for the current request. If in  a Transfer/Execute call the transferred Path is returned. 
~/admin/paths.aspx
PathInfo Returns any extra path following the script name. If no extra path is provided returns the root-relative path (returns text in red below). string.Empty if no PathInfo is available. 
/webstore/admin/paths.aspx/ExtraPathInfo
RawUrl Returns the full root relative relative URL including querystring and extra path as a string. 
/webstore/admin/paths.aspx?sku=wwhelp40
Url Returns a fully qualified URL including querystring and extra path. Note this is a Uri instance rather than string. 
http://www.west-wind.com/webstore/admin/paths.aspx?sku=wwhelp40
UrlReferrer The fully qualified URL of the page that sent the request. This is also aUri instance and this value is null if the page was directly accessed by typing into the address bar or using an HttpClient. Based Referrer client Http header. 
http://www.west-wind.com/webstore/default.aspx?Info
Control.TemplateSourceDirectory Returns the logical path to the folder of the page, master or user control on which it is called. This is useful if you need to know the path only to a Page or control from within the control. For non-file controls this returns the Page path. 
/webstore/admin/

As you can see there’s a ton of information available there for each of the three common path formats:

  • Physical Path 
    is an OS type path that points to a path or file on disk. 
  • Logical Path 
    is a Web path that is relative to the Web server’s root. It includes the virtual plus the application relative path. 
  • ~/ (Root-relative) Path 
    is an ASP.NET specific path that includes ~/ to indicate the virtual root Web path. ASP.NET can convert virtual paths into either logical paths using Control.ResolveUrl(), or physical paths using Server.MapPath(). Root relative paths are useful for specifying portable URLs that don’t rely on relative directory structures and very useful from within control or component code.

You should be able to get any necessary format from ASP.NET from just about any path or script using these mechanisms.

~/ Root Relative Paths and ResolveUrl() and ResolveClientUrl()

ASP.NET supports root-relative virtual path syntax in most of its URL properties in Web Forms. So you can easily specify a root relative path in a control rather than a location relative path:

<asp:Image runat="server" ID="imgHelp"  ImageUrl="~/images/help.gif" />

ASP.NET internally resolves this URL by using ResolveUrl("~/images/help.gif") to arrive at the root-relative URL of /webstore/images/help.gif which uses the Request.ApplicationPath as the basepath to replace the ~. By convention any custom Web controls also should use ResolveUrl() on URL properties to provide the same functionality.

In your own code you can use Page.ResolveUrl() or Control.ResolveUrl() to accomplish the same thing:

string imgPath = this.ResolveUrl("~/images/help.gif");
imgHelp.ImageUrl = imgPath;

Unfortunately ResolveUrl() is limited to WebForm pages, so if you’re in an HttpHandler or Module it’s not available.

ASP.NET Mvc also has it’s own more generic version of ResolveUrl in Url.Decode:

<script src="<%= Url.Content("~/scripts/new.js") %>" type="text/javascript"></script> 

which is part of the UrlHelper class. In ASP.NET MVC the above sort of syntax is actually even more crucial than in WebForms due to the fact that views are not referencing specific pages but rather are often path based which can lead to various variations on how a particular view is referenced.

In a Module or Handler code Control.ResolveUrl() unfortunately is not available which in retrospect seems like an odd design choice – URL resolution really should happen on a Request basis not as part of the Page framework. Luckily you can also rely on the static VirtualPathUtility class:

string path = VirtualPathUtility.ToAbsolute("~/admin/paths.aspx");

VirtualPathUtility also many other quite useful methods for dealing with paths and converting between the various kinds of paths supported. One thing to watch out for is that ToAbsolute() will throw an exception if a query string is provided and doesn’t work on fully qualified URLs. I wrote about this topic with a custom solution that works fully qualified URLs and query strings here (check comments for some interesting discussions too).

Similar to ResolveUrl() is ResolveClientUrl() which creates a fully qualified HTTP path that includes the protocol and domain name. It’s rare that this full resolution is needed but can be useful in some scenarios.

Mapping Virtual Paths to Physical Paths with Server.MapPath()

If you need to map root relative or current folder relative URLs to physical URLs or you can use HttpContext.Current.Server.MapPath(). Inside of a Page you can do the following:

string physicalPath = Server.MapPath("~/scripts/ww.jquery.js"));

 

MapPath is pretty flexible and it understands both ASP.NET style virtual paths as well as plain relative paths, so the following also works.

string physicalPath = Server.MapPath("scripts/silverlight.js");

as well as dot relative syntax:

string physicalPath = Server.MapPath("../scripts/jquery.js");

Once you have the physical path you can perform standard System.IO Path and File operations on the file. Remember with physical paths and IO or copy operations you need to make sure you have permissions to access files and folders based on the Web server user account that is active (NETWORK SERVICE, ASPNET typically).

Server and Host Information

Between these settings you can get all the information you may need to figure out where you are at and to build new Url if necessary. If you need to build a URL completely from scratch you can get access to information about the server you are accessing:

Server Variable Function and Example
SERVER_NAME The of the domain or IP Address 
wwww.west-wind.com or 127.0.0.1
SERVER_PORT The port that the request runs under. 
80
SERVER_PORT_SECURE Determines whether https: was used. 
0 or 1
APPL_MD_PATH ADSI DirectoryServices path to the virtual root directory. Note that LM typically doesn’t work for ADSI access so you should replace that with LOCALHOST or the machine’s NetBios name. 
/LM/W3SVC/1/ROOT/webstore

 

 

Request.Url and Uri Parsing

If you still need more control over the current request URL or  you need to create new URLs from an existing one, the current Request.Url Uri property offers a lot of control. Using the Uri class and UriBuilder makes it easy to retrieve parts of a URL and create new URLs based on existing URL. The UriBuilder class is the preferred way to create URLs – much preferable over creating URIs via string concatenation.

Uri Property Function
Scheme The URL scheme or protocol prefix. 
http or https
Port The port if specifically specified.
DnsSafeHost The domain name or local host NetBios machine name 
www.west-wind.com or rasnote
LocalPath The full path of the URL including script name and extra PathInfo. 
/webstore/admin/paths.aspx
Query The query string if any 
?id=1

The Uri class itself is great for retrieving Uri parts, but most of the properties are read only if you need to modify a URL in order to change it you can use the UriBuilder class to load up an existing URL and modify it to create a new one.

Here are a few common operations I’ve needed to do to get specific URLs:

Convert the Request URL to an SSL/HTTPS link

For example to take the current request URL and converted  it to a secure URL can be done like this:

UriBuilder build = new UriBuilder(Request.Url);
build.Scheme = "https";
build.Port = -1;  // don't inject port

Uri newUri = build.Uri;
string newUrl = build.ToString();

Retrieve the fully qualified URL without a QueryString 
AFAIK, there’s no native routine to retrieve the current request URL without the query string. It’s easy to do with UriBuilder however:

UriBuilder builder = newUriBuilder(Request.Url); 
builder.Query = ""
stringlogicalPathWithoutQuery = builder.ToString();

What else?

I took a look through the old post’s comments and addressed as many of the questions and comments that came up in there. With a few small and silly exceptions this update post handles most of these.

But I’m sure there are a more things that go in here. What else would be useful to put onto this post so it serves as a nice all in one place to go for path references? If you think of something leave a comment and I’ll try to update the post with it in the future.

ASP.NET

Useful properties (content repost)

1. March 2010

As a resource for myself I found this blog entry and wanted to retain the information as a reference list. Thanks to SheoNarayan for this post. Which you can find here: http://www.dotnetfunda.com/articles/article79.aspx

 

Reposting in case that site dies:

System.Web.HttpRequest (Request) object is used to get the information about the user. In this article, I am trying to show some useful properties of Request object that is very handy and useful while developing ASP.NET applications.

I have presented this information by taking example of a url http://localhost/myfunda/test.aspx?q=search&re=afdsf where "localhost" is my local IIS, "myfunda" is a virtual directory name and "test.aspx" page is the name of the page where the request object property has been written.

Follwing example has been show in the format of

Request Object: Result - Description

Request object

Request.Path: /myfunda/test.aspx - Is used to get information about the path of the url from where the request is being made.

Request.Cookies: - Get the collection of cookies, To know more about cookie, refer to http://www.dotnetfunda.com/articles/article61.aspx

Request.ContentLength: 0 - Returns the length of content sent by the browser.

Request.IsLocal: True - Returns the value indicating whether the request is from local computer.

Request.IsSecureConnection: False - Returns the value indicating whether the connection is secure (using HTTPS).

Request.PhysicalApplicationPath: C:\Web\MyFunda.Web\ - Returns physical application path.

Request.PhysicalPath: C:\Web\MyFunda.Web\test.aspx - Returns physical path of the file being requested.

Request.QueryString: q=search&re=afdsf - Returns all querystrings excluding ?. 

Request.RawUrl: /myfunda/test.aspx?q=search&re=afdsf - Returns the url (including the virtual directory if any) of the page along with querystring.

Request.UserHostAddress: 60.243.141.170 - Returns the ip address of remote client.

Request.UserHostName: 60.243.141.170 - Returns the host name (DNS Name) if specified or returns the ip address of the remote client.

Request.ApplicationPath: /myfunda - Returns the virtual application root path.

Request.Url: http://localhost/myfunda/test.aspx?q=search&re=afdsf - Returns the complete url of the current request as appears in web browser address bar.

Request.Url object

Request.Url.AbsolutePath: /myfunda/test.aspx - Returns the absolute path (no querystrings)

Request.Url.AbsoluteUri: http://localhost/myfunda/test.aspx?q=search&re=afdsf - Returns the absolute URI, almost same as Request.Url.

Request.Url.Authority: localhost - Returns the IP Host name or IP Address of the server.

Request.Url.Host: localhost - Returns the host of this instance. 

Request.Url.HostNameType: Dns - Gets the type of the host specified in the uri.

Request.Url.IsDefaultPort: True - Gets whether the port value for the URI is default.

Request.Url.IsFile: False - Gets information whether the specified uri is the file uri.

Request.Url.LocalPath: /myfunda/test.aspx - Gets local file path name

Request.Url.OriginalString: http://localhost:80/myfunda/test.aspx?q=search&re=afdsf - Returns original uri passed to the uri constructor.

Request.Url.PathAndQuery: /myfunda/test.aspx?q=search&re=afdsf - Returns the path and querystring of the uri.

Request.Url.Port: 80 - Gets the port number of the uri.

Request.Url.Query: ?q=search&re=afdsf - Gets the querystring of the uri along with ?

Request.Browser object

Request.Browser.BackgroundSounds: True - To know whether browser supports background sound.

Request.Browser.Beta: False - To know whether current version is in Beta release.

Request.Browser.Browser: AppleMAC-Safari - Get the browser string (if any).

Request.Browser.CanInitiateVoiceCall: False - Get a value indicating whether browser can initiate voice call.

Request.Browser.CanSendMail: True - Get a value indicating whether browser can send email using mailto: .

Request.Browser.Cookies: True - Get a value indicating whether the browser supports cookie.

Request.Browser.Crawler: False - Get a value indicating whether the browser is a search engine web crawler.

Request.Browser.DefaultSubmitButtonLimit: 1 - Get the maximum number of submit button allowed in a form.

Request.Browser.Frames: True - Get a value whether browser supports Frames.

Request.Browser.HasBackButton: True - Get a value whether browser has a dedicated back button.

Request.Browser.Id: safari1plus - Get the internal identifier of the browser as specified in tbe browser definition file.

Request.Browser.InputType: keyboard - Returns the type of input supported by the browser.

Request.Browser.IsColor: True - Get a value indicating whether the browser has a color display.

Request.Browser.IsMobileDevice: False - Get a value indicating whether the browser is a mobile device (if recognized).

Request.Browser.JavaApplets: True - Get a value indicating whether the browser supports Java applets.

Request.Browser.JavaScript: True - Get a value indicating whether the browser supports JavaScript.

Request.Browser.EcmaScriptVersion: 1.4 - Get the version number of Ecma script supported by browser.

Request.Browser.JScriptVersion: 0.0 - Get the version of the JScript supported by the browser.

Request.Browser.MajorVersion: 5 - Get the major version of the browser.

Request.Browser.MinorVersion: 0 - Get the minor version of the browser.

Request.Browser.MaximumHrefLength: 10000 - Get maximum number of characters supported for href (hrml link) element 

Request.Browser.MaximumRenderedPageSize: 20000 - Get maximum length of the page in byte browser can display.

Request.Browser.MaximumSoftkeyLabelLength: 5 - Get maximum length of the text that a soft key label can display.

Request.Browser.MobileDeviceManufacturer: Unknown - Get the name of the mobile device manufacturer, if identifiable.

Request.Browser.MobileDeviceModel: Unknown - Get the name of the model number of mobile device , if identifiable.

Request.Browser.Platform: WinXP - Get the name of the platform client is using.

Request.Browser.PreferredRenderingType: html32 - Get the name of the type of content browser prefer to get.

Request.Browser.ScreenBitDepth: 24 - Get the depth of the display, a bits per pixel.

Request.Browser.ScreenCharactersHeight: 40 - Returns the approximate height of the display in lines.

Request.Browser.ScreenCharactersWidth: 80 - Returns the approximate width of the display in characters.

Request.Browser.ScreenPixelsHeight: 480 - Returns the approximate height of the screen in pixel.

Request.Browser.ScreenPixelsWidth: 640 - Returns the approximate width of the screen in pixel.

Request.Browser.SupportsBold: True - Gets a value indicating whether browser supports <b> html element.

Request.Browser.SupportsCacheControlMetaTag: True - Gets a value indicating whether browser supports cache control value in the META element.

Request.Browser.SupportsCss: True - Gets a value indicating whether browser supports CSS.

Request.Browser.SupportsDivAlign: True - Gets a value indicating whether browser supports align attribute of the <div> html element.

Request.Browser.SupportsEmptyStringInCookieValue: True - Gets a value indicating whether browser supports null in cookie values.

Request.Browser.SupportsFontColor: True - Gets a value indicating whether browser supports color attribute in <font> html element. In the same way, we have for font size, font name etc.

Request.Browser.SupportsImageSubmit: True - Gets a value indicating whether browser supports submission of form using a custom image in place of a button.

Request.Browser.SupportsQueryStringInFormAction: True - Gets a value indicating whether browser supports querystring in the action attribute of the <form> html element.

Request.Browser.SupportsSelectMultiple: True - Gets a value indicating whether browser supports multiple selection in the <select> html element.

Request.Browser.SupportsXmlHttp: False - Gets a value indicating whether browser supports receiving xml over HTTP.

Request.Browser.Tables: True - Gets a value indicating whether browser supports receiving <table> html element.

Request.Browser.Type: Desktop - Can be used to Get name and major version of the browser.

Request.Browser.VBScript: False - Gets the value whether browser supports VBScript .

Request.Browser.Version: 5.0 - Gets the full version of the browser.

Request.Browser.Win32: True - Gets the value indicating whether the client is using Win32 based system.

There of course many more properties than I have conver in this article, however I felt these are most frequently used and useful to know. Thanks for reading and please add other useful information related to Request if you have by responding to this article.

 

ASP.NET