ASP 5 MVC 6 Server.MapPath equivalent [duplicate] - asp.net-mvc

I know I can get WebRoot by HostingEnvironment (Microsoft.AspNet.Hosting namespace).
I need to get a physical path according to a virtual path created in IIS within my web application. In IIS, the website root points to wwwroot of my published site and there is a virtual directory added in IIS which points to a folder outside of my wwwroot. I hope I can get the physical path of that virtual directory. In MVC 5 or earlier version, I can use HostingEnvironment.MapPath (System.Web namespace) or Server.MapPath, what should I do in MVC 6?
EDIT:
It's not the virtual path but the virtual directory added in IIS. I hope I can get the physical path of that virtual directory. I think virtual directory is a particular feature of IIS, which looks like a sub path of a full virtual path but can be a folder outside of physical web root folder.
Oct 4, 2015
Refer to this issue in ASP.NET 5 Hosting repo, So far, it doesn't seem we can get the physical path of a virtual directory in IIS.

You can use IApplicationEnvironment for that, which contains the property ApplicationBasePath
private readonly IApplicationEnvironment _appEnvironment;
public HomeController(IApplicationEnvironment appEnvironment)
{
_appEnvironment = appEnvironment;
}
public IActionResult Index()
{
var rootPath = _appEnvironment.ApplicationBasePath;
return View();
}

On Azure Web Apps, you'll find the wwwroot is in a different place:
All of the source code gets copied to D:/Home/site/approot/src/{WebsiteName} except the wwwroot. IApplicationEnvironment.ApplicationBasePath points here.
The wwwroot goes in D:/Home/site/wwwroot. IHostingEnvironment.WebRootPath points here.
It still won't work with IIS virtual directories. There is a WebRootFileProvider property on the IHostingEnvironment interface which provides an IFileProvider with methods like GetFileInfo(string subpath). I read the code and found subpath is a physical one, not a virtual one.
Obviously, in a self-hosted setup, there is no IIS. If you rely on IIS and really must do this, you'll probably have to reference System.Web.
HTH.

Related

Not able to access files outside ASP.net Core application using Virtual Directory

I have hosted ASP.net Core application in IIS, I have added a virtual directory into the wwwroot\images folder. But I am not able to access the contents in the folder. Can anyone help me? I need to share images between multiple ASP.net Core sites, so I tried using Virtual directory.
I fased same problem couple of weeks ago.
If your project is crossplatform you need to use virtual folders like this:
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(#"D:\files"),
RequestPath = new PathString("/files"),
EnableDirectoryBrowsing = false // you make this true or false.
});
seting in iis will not work

Accessing a Virtual Directory from a Web API

I have a folder of files (form templates) that need to be accessed from both a .NET Web API application and a separate .NET MVC application. The folder for these form templates is outside of the websites. Ideally, I'd like to be able to store the path in the Web.Config files so that the applications can be easily tested in a local environment.
I tried setting up virtual directories on the hosted site but couldn't figure out how to reference them in the Web API. I tried several means of referencing the Virtual Directory that did not work. Most posts suggested trying Server.MapPath("MyVirtualDirectory") but that returns "....\wwwroot\MyApiController\Action\MyVirtualDirectory", not the physical path of the virtual directory.
I removed the Virtual Directory and attempted to "navigate" to the correct path but was blocked by "Cannot use a leading .. to exit above the top directory".
So what is the correct way to access a resource using a virtual directory in .NET Web API application? Is the same method going to work for the .NET MVC application?
You need to use HostingEnvironment, like:
public static string MapPath(string path){
string result;
result = HostingEnvironment.MapPath(path);
return result;
}
Additionally HostingEnvironment provides features like ApplicationPhysicalPath:
result = HostingEnvironment.ApplicationPhysicalPath + "App_Data\\somefile.xml";

Access 'parent' application from sub virtual directory

Assume I have two MVC applications, Public and Admin:
Public is a virtual directory mapped to www.mydomain.com
Admin is a virtual directory mapped to admin.mydomain.com.
Under the hood the files are organised as follows:
/ (all my public site files are here)
/Assets (a bunch of public assets like images .. for example)
/Admin (all my files for admin.mydomain.com are here).
The question is, can I access /Assets from within the admin.mydomain.com application.
Thanks.
You can create /Assets as virtual directory and that can be accessed using URI

MVC calling views from other virtual directories

I'm trying to create a 'shared' MVC application, which can be used for all common data that is used by other MVC and legacy Web applications.
For example, I have two Virtual Directories setup on my server:
mysite.com/Report
mysite.com/Base
So what I'd like to be able to do is this to include a view from the 'base' folder in the 'Report' one;
#Html.Partial("/Base/Views/Shared/_NavigateMenu.cshtml")
Note that ~ in Report will go to mysite.com/Report, I cant use that.
However doing so results in the following exception:
The virtual path '/Base/Views/Shared/_NavigateMenu.cshtml' maps to another application, which is not allowed.
I know about areas however there are a lot of downsides in using them, firstly I cant (or is very difficult) to migrate the existing legacy applications into a area, also logistically it is nicer to have each MVC application separated and have a space for common objects. (Not just views, but css, images, etc.)
So the question is how do I 'allow' MVC to map and thus access to other applications?
Trying to share a MVC aplication is basically trying to share several different components :
Controllers should be shared through a separate class library, reachable through area registrations
Models can be in any referenced class library
for views, I assume base Base application root folder contains the application web.config. Views, hosting the views, can contain a web.config relative to views rendering (say for razor configuration)
To use Views in application Report, you have to create a virtual directory SharedViews (or whatever name you want) in Report, which will point to the physical path of Base/Views.
Then you will be able to write :
#Html.Partial("~/SharedViews/Shared/_NavigateMenu.cshtml")
The points are :
having a virtual directory in Report, to avoid switching from an app to another
having this virtual directory not reference the root directory of Base, to avoid app configuration conflicts
you can resolve this problem using the char ~ before virtual path i.e.
#Html.Partial("~/Base/Views/Shared/_NavigateMenu.cshtml")
or else
if it is specified path set up as a Virtual Directory in IIS than it may treat it as another application even though it's in the same directory as the main application.

Moving MVC Beta Web application to Virtual Directory strips out all /Content resources

I have a very nice MVC Beta application developed using VS2008 on a win2008 server. My troubles began when I attempted to deploy it to an IIS6 virtual directory. After installing the MVC Beta on the target win2003 server box I was able to get the application to display from a virtual directory, but all images and css attributes are missing. The place holders suggest the images should be located at the root web site rather than the virtual directory the application is running out of.
Actual path:
http://localhost/Content/images/<myimage>.png
Rather than:
http://localhost/<virtualdirectory>/Content/images/<myimage>.png
If I install the application in its own web site (Root) everything works fine. I can duplicate this in VS2008 by setting the web properties to "Use Visual Studio Development Server"/Specific Port / and add a virtual path.
I can also duplicate this by setting web properties on the project to "Use Local IIS Web Server" and creating a virtual directory.
There is something obvious I am missing regarding mvc url routing. Any guidance here would be appreciated.
How have you referenced the images? They should start "~/" to mean "app root" ("/" is "site root") - i.e. "/Content/images/myimage.png".
However, some controls will send this to the client without substituting the virtual path; for a related thread (talking about js, but the concept is the same), see here (includes a simple solution / example approach).
Also - is the virtual an app? cog icon...
Consider a <base href...> tag in the master page (to the app root)

Resources