Not able to access files outside ASP.net Core application using Virtual Directory - 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

Related

ASP.Net MVC Directory Structure

I'm new to ASP.NET MVC and I am using and I am using Visual Studio Community 2019. I created the "Hello World" website using the wizard. The file structure looks like the image below. I have found information on this but image of the directory structure never seems to have the wwwroot directory.
What would be the best practice for where to put the Content folder which would then contain an image folder and possibly a documents folder?
wwwroot is the default folder for serving static files. See the docs here. So yes, it is the correct place to put images and documents.
As per the docs you can create more locations to serve static files from. e.g this serves files from the StaticFiles directory.
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
RequestPath = "/StaticFiles"
});
This is of course assuming they will be served statically. If you needed to authenticate or anything when looking at docs/files you would need to implement a different solution.

ASP 5 MVC 6 Server.MapPath equivalent [duplicate]

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.

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";

how to deploy ASP.Net MVC 4 application on IIS with bin folder

I have tried these steps but could not make it. When I browse my app it shows dll file.
Log onto the machine that is or will be hosting your application.
Use IIS Manager to create a new website for your application.
Create a new application in that site. I believe this also will automatically create an application pool with the same name for you and use it by default.
Specify the virtual directory for your application. This is going to tell IIS where to look for your mvc application. For this case lets assume it is C:\myApp
On your own machine Build the application however you build it with the correct solution configuration (i.e. Release mode). Lets say the result of your build is located at C:\MyProject\bin.
Copy C:\MyProject\bin from your machine onto your hosting machine at C:\myApp
I am a novice to this technology.
you shouldn't just drag the bin folder. it is everything else too like the images, css, jscript files as well as the cshtml files too (your views) to the c:\myapp folder.
or perhaps just do a publish within visual studio. maybe even take a look at this to see if this helps:
How to publish my MVC 3 web application onto IIS7
but generally speaking, I build the solution. I then create the vdir in my IIS. I copy the bin and view folder along with images/css/jscript/shared folders etc... to C:\my deployed site. I then convert to application for that vdir I just created in IIS.
You have to use publish action for ASP.Net web application(MVC, Forms and etc.)

Accessing static content of an ASP.Net MVC project with IIS7

I've create a web site on my local IIS 7 with my own ASP.Net MVC project on its root.
Everything is working fine except for the static content. Going to http://localhost:8080/Content/Site.css gives me a 404. I can see the folder on IIS Manager.
The content is served fine with the small development server you get when you run the application on Visual Studio 2008. Any ideas what might be wrong?
The problem was permissions. Even though when I create the IIS7 web site I told it to access the files as my user (it wouldn't work at all otherwise), for static file it was using the user of the application pool. Giving access to IIS APPPOOL\MyApplication to the folder where my project was fixed the issue.
How about
routes.RouteExistingFiles = true;
in your Global.asax?
Try going to http://localhost:8080/../../Content/Site.css, not sure if your original URL is matching a route.
Not really a programming question though.

Resources