how can an .ASPX page get its file system path? - asp.net

I have a page something.aspx, with associated codebehind something.aspx.cs. In that codebehind, I want to know the filesystem location of something.aspx. Is there any convenient way to get it?
Update: I got several excellent answers, which unfortunately didn't work because of something else crazy I'm doing. I'm encoding some additional information on the URL I pass in, so it looks like this:
http://server/path/something.aspx/info1/info2/info3.xml
The server deals with this OK (and I'm not using querystring parameters to work around some other code that I didn't write). But when I call Server.MapPath(Request.Url.ToString()) I get an error that the full URL with the 'info' segments isn't a valid virtual path.

// File path
string absoluteSystemPath = Server.MapPath("~/relative/path.aspx");
// Directory path
string dir = System.IO.Path.GetDirectoryName(absoluteSystemPath);
// Or simply
string dir2 = Server.MapPath("~/relative");

Request.PhysicalPath

Server.MapPath is among the most used way to do it.
string physicalPath = Server.MapPath(Request.Url);

Server.MapPath( Request.AppRelativeCurrentExecutionFilePath )

Related

How to write a link to an Alfresco Share folder? (within a site)

I want to create an HTTP link to a particular folder in Alfresco Share.
Alfresco Share encodes paths in a rather convoluted way:
thesite
http://server/share/page/site/thesite/documentlibrary
thesite/documentLibrary/somefolder/anotherfolder
http://server/share/page/site/thesite/documentlibrary#filter=path|%2Fsomefolder%2Fanotherfolder
thesite/documentLibrary/éß和ệ
http://server/share/page/site/s1/documentlibrary#filter=path|%2F%25E9%25DF%25u548C%25u1EC7
thesite/documentLibrary/a#bc/éß和ệ
http://server/share/page/site/thesite/documentlibrary#filter=path%7C%2Fa%2523bc%2F%25E9%25DF%25u548C%25u1EC7%7C
I suspect it is a double URLencode, with the exception of slashes which are only URLencoded once.
But I am not 100% sure.
Is there a C# library that does this encoding?
You might find it easier to use the legacy ?path= parameter for document library folder URL creation.
Using the example path /Sample Content/∞⊕⊗¤☺☹★☆★☆★ instead of building a URL of the form
http://alfresco.example/share/page/site/mike/documentlibrary#filter=path%7C%2FSample%2520Content%2F%25u221E%25u2295%25u2297%25A4%25u263A%25u2639%25u2605%25u2606%25u2605%25u2606%25u2605%7C&page=1
you can generate one that looks like
https://alfresco.example/share/page/site/mike/documentlibrary?path=/Sample%20Content/%E2%88%9E%E2%8A%95%E2%8A%97%C2%A4%E2%98%BA%E2%98%B9%E2%98%85%E2%98%86%E2%98%85%E2%98%86%E2%98%85
So using C# you'd use Uri.EscapeUriString() on the path and append it to the base document library URL with ?path=
You can see how the path parameter is interpreted in documentlibrary.inc.ftl
Check org.alfresco.repo.web.scripts.site.SiteShareViewUrlGetfor an implementation. Its not very elegant, but seems pretty complete, probably a good starting point for your own class.
There really should be a helper class for this, but maybe I'm missing something.
I could not find a library, so I wrote the following code:
if (path.Contains("documentLibrary"))
{
int firstSlashPosition = path.IndexOf("/");
string siteName = path.Substring(0, firstSlashPosition);
string pathWithinSite = path.Substring(firstSlashPosition + "/documentLibrary".Length);
string escapedPathWithinSite = HttpUtility.UrlEncode(pathWithinSite);
string reescapedPathWithinSite = HttpUtility.UrlEncode(escapedPathWithinSite);
string sharePath = reescapedPathWithinSite.Replace("%252f", "%2F");
return "http://server/share/page/site/" + siteName + "/documentlibrary#filter=path|" + sharePath;
}
else
{
// Site name only.
return "http://server/share/page/site/" + path + "/documentlibrary";
}
Any correction or better answer would be much appreciated!

how can get image from a relative path

i am working in asp .net mvc3.
i want to get a image which exist in this location in my project
G:\projects\CalcoWoms\CalcoWOMS\Content\pictures\calcologo.png
CalcoWOMS is my project name. i want to fetch this calcologo.png in following line please check following line and tell me how should write this following line in correct way.?
iTextSharp.text.Image gif = iTextSharp.text.Image.GetInstance("~/calcologo.png");
means in place of ("~/calcologo.png"); what path i should write ?
You could use MapPath
var physicalPath = Server.MapPath("~/Content/pictures/calcologo.png");
You could try "%~dp0calcologo.png".
%~dp0 means current directory.
You could use the HostingEnvironment object along with Path.Combine
Path.Combine(#HostingEnvironment.ApplicationPhysicalPath, "calcologo.png");
Of course, #HostingEnvironment.ApplicationPhysicalPath will only take you to the root of your application, so you might need to use "Content/picturescalcologo.png".

ASP.NET: Get *real* raw URL

In ASP.NET, is there any way to get the real raw URL?
For example, if a user browse to "http://example.com/mypage.aspx/%2F", I would like to be able to get "http://example.com/mypage.aspx/%2F" rather than "http://example.com/mypage.aspx//".
I would of course like a clean way to do it, but I can live with a hacky approach using reflection or accessing obscure properties.
At the moment, I try to use the uri in the Authorization-header (which works), but I cannot rely on that always being there.
EDIT:
What I really want to do is to be able to distinguish between "http://example.com/mypage.aspx/%2F" and "http://example.com/mypage.aspx/%2F%2F".
It looks like ASP.NET first converts "%2F%2F" into "//" and then converts the slashes into a single slash.
So just re-encoding it is not going to work.
I wasn't able to test this because it only works in IIS and not the ASP.NET Development Server that is part of Visual Studio, but try:
Request.ServerVariables[ "HTTP_URL" ]
The following code works for me:
IServiceProvider serviceProvider = (IServiceProvider)HttpContext.Current;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)serviceProvider.GetService(typeof(HttpWorkerRequest));
string realUrl = workerRequest.GetServerVariable("HTTP_URL");
Note that this only works when running on the IIS and not under f.x. ASP.NET Development Server!
Thanks to Lucero for the answer in another thread and Zhaph for pointing me to the thread.
See also:
Get the exact url the user typed into the browser
Server.HtmlEncode(Request.RawUrl);
The raw URL is defined as the part of the URL following the domain information. In the URL string http://www.contoso.com/articles/recent.aspx, the raw URL is /articles/recent.aspx. The raw URL includes the query string, if present.
see also:link text
I can't test here, but this might be what you need:
Request.Url.AbsoluteUri
Request.RawUrl will return the application relative path(including querystring info) while Request.Url will return the complete path(including querystring info).
For more information, see "Making sense of ASP.NET paths".
Well, you could just encode it back to the url-encoded version.
Get the url from the request and urlencode only the query string part and then concatenate them

Asp.Net System.Web.Routing Find actual .aspx Page

I'm using System.Web.Routing to have some better URL's and have come across a problem. I need to know the actual page that's handling the request.
for example a request comes in as:
/basketball/home
I need to find the page that handles that request, like:
/management/default.aspx
I'm only using the System.Web.Routing and not MVC. I have a handle to the RequestContext that contains some of the route information, but i don't see what i need.
Thanks in advance.
******* UPDATE *******
I was able to use Context.CurrentHandler which give me "ASP.management_default_aspx", not exactly the page but enough to get the page name.
There is actually another simple way to get the actual page:
String vPath = ((System.Web.Routing.PageRouteHandler)Page.RouteData.RouteHandler).VirtualPath
Do not forget to check Page.RouteData.RouteHandler is not null - while you are getting the page w/o ASP.Net routing but directly.
Can you not retrieve this from the current HttpContext object?
Perhaps something like this:
public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}
UPDATE:
Have you tried this article?
How to: Construct a URL from a Route
You should be able to retrieve it back from the Routing table you have constructed.
vhinn terrible's answer worked...
Page.AppRelativeVirtualPath
You just have to remove the initial tilde ("~"), and you're ready to go.
var path = Page.AppRelativeVirtualPath.Replace("~", String.Empty);
I don't know why it was downvoted. Worked for me like a charm.
Try using this code:
Page.AppRelativeVirtualPath
I was able to use Context.CurrentHandler which give me "ASP.management_default_aspx", not exactly the page but enough to get the page name.

What is the most efficient way to perform the reverse of Server.MapPath in an ASP.Net Application

I am building an MVC application in which I am reading a list of files from the file system and I want to pass the relative URL to that file to the view, preferably prefixed with "~/" so that whatever view is selected cab render the URL appropriately.
To do this, I need to enumerate the files in the file system and convert their physical paths back to relative URLs. There are a few algorithms I've experimented with, but I am concerned about efficiency and minimal string operations. Also, I believe there's nothing in the .Net Framework that can perform this operation, but is there something in the latest MVC release that can?
At the moment I don't know any built-in method to do it, but it's not difficult, I do it like this:
We need to get the Application root, and replace it in our new path with ~
We need to convert the backslashes to slashes
public string ReverseMapPath(string path)
{
string appPath = HttpContext.Current.Server.MapPath("~");
string res = string.Format("~{0}", path.Replace(appPath, "").Replace("\\", "/"));
return res;
}
Isn't this what UrlHelper.Content method does? http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.content.aspx
I did some digging, trying to get the UrlHelper class to work outside of a controller, then I remembered a old trick to do the same thing within an aspx page:
string ResolveUrl(string pathWithTilde)
Hope this helps!
See:
https://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl(v=vs.110).aspx

Resources