Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you resolve a virtual path to a file under an OWIN host?

Tags:

c#

owin

Under ASP.NET and IIS, if I have a virtual path in the form "~/content", I can resolve this to a physical location using the MapPath method:

HttpContext.Server.MapPath("~/content"); 

How can you resolve a virtual paths to a physical location under an OWIN host?

like image 442
Paul Turner Avatar asked Jul 04 '14 09:07

Paul Turner


People also ask

What is a virtual file path?

Virtual Path or Relative Virtual Path: The path that the application identifies or is identified by from its Web server. For instance, in IIS (or OWIN) you may have a resource directory for your images in folder c:\\inetpub\ftp\images but the developer maps this folder to the app like so... ~\Images .

What is virtual path in C#?

Virtual path. This is the logical path to access the file which is pointed to from outside of the IIS application folder. Let's display this image from Hard-drive 'E:' using a virtual directory in IIS default web site.


2 Answers

You may use AppDomain.CurrentDomain.SetupInformation.ApplicationBase to get root of your application. With the root path, you can implement "MapPath" for Owin.

I do not know another way yet. (The ApplicationBase property is also used by Microsoft.Owin.FileSystems.PhysicalFileSystem.)

like image 186
TN. Avatar answered Oct 11 '22 12:10

TN.


You shouldn't use HttpContext.Server as it's only available for MVC. HostingEnvironment.MapPath() is the way to go. However, it's not available for self-hosting owin. So, you should get it directly.

var path = HostingEnvironment.MapPath("~/content"); if (path == null) {     var uriPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);     path = new Uri(uriPath).LocalPath + "/content"; } 
like image 34
Boris Lipschitz Avatar answered Oct 11 '22 12:10

Boris Lipschitz