Access 'parent' application from sub virtual directory - asp.net-mvc

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

Related

Is everyone allowed to access rails public folder via URL?

When assets like images or files are in the rails public directory with asset pipeline enabled, is everyone allowed to access them through URL?
I am asking this because now I am learning about implementing File Uploading through Carrierwave with the help of the book, Rails 4 in Action, and it says the files should be moved outside the public folder for access control.
If assets are in the public folder, does it mean that we can't do access control?
Any one can access the contents of public folder through URL.
Ref:
Are files in public folder accessible to outside world? - Rails

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

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.

Are files in public folder accessible to outside world? - Rails

This is a simple question but I can't seem to find an answer for it anywhere. If you store some files (say some static PDFs) in your public directory, is there a way that someone who isn't authorized to view those files, can view them by typing in a url like example.com/public/static_document.pdf? If so, can you disable this in Rails?
The public is definitely public and open to people guessing the URL.
Check out Ruby On Rails - Securing Downloads Area for someone else asking similar.
I store these generally in Rails.root/secure_files and then use send_file in the Controller to authorize and send these files.
The public folder contains the static files and compiled assets for the client to read. The folder by default is accessible to anyone visiting your site. Test it by typing in a slug of the folder name currently in your public folder.

Resources