Access server path of web site from within assembly library - asp.net-mvc

If I have an assembly library (dll) that is referenced from within an ASP.NET MVC app, is it possible for the assembly to get to the server path without having to pass it in from the MVC app, or have access to HttpContext?
If I use a relative path, it will return the processes path. In the case of running locally in debug, it is:
C:\Program Files\IIS Express
However, the file lives in the bin output, with this DLL, and the rest of the site's binaries.
Is my only choice to pass the path in from the web app?
UPDATE
For now, I'm just using HttpContext.Current.Server.MapPath(), but I don't like having to use HttpContext, in the event that we consume this API from something other than a web app.

To get the IIS server path that holds the web site you need HttpContext.Request.FilePath.
I pass the HttpContext into my library function, when I need to know where the web site is located.
However if you want to know the DLL's installed location, then I have seen it done using Assembly.Location

Related

URL Routes In IIS7 MVC 5 (Single Paged Application)

After publishing a MVC5 web application of mine to my IIS server (Individual User Accounts), it would seem that the URL is accessed incorrectly.
During debug, it would be e.g http://localhost:1234/api/Account/UserInfo?=XXXXX
The debug works just fine. The only issue kicks in after I've published it via my IIS7 server.
After publishing and using Google Chrome's console, it would appear that the page is requesting for a resource at mydomainname.com/api/Account/UserInfo?=XXXX instead of mydomainname.com/WEBAPPLICATIONNAME/api/Account/UserInfo?=XXXX.
My best guess is to modify the URLs in /Scripts/app/app.datamodel.js but it would just cause more parsing problems.
I've searched around and can't seem to find any related problems. I hope someone here will be able to lend a hand.
Look like you are using relative path like "/api/Account/UserInfo". Instead i'll recommend you to use #Url.Content("/api/Account/UserInfo"). This will solve your problem
Explanation
In local system when we run application in WebDev server it never have sub folder (like WEBAPPLICATIONNAME) therefore you relative path work correctly. but when you host your application in IIS under Default website in another new website /Virtual folder (like 'WEBAPPLICATIONNAME') then "/api/Account/UserInfo" fall back to Default Website because for '/' in starting. #Url.Content or #Url.Action make sure to add virtual directory name, hence changing your path to "/WEBAPPLICATIONNAME/api/Account/UserInfo" in IIS.

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 do i host classic asp files within MVC application

Is it possible to render classic asp files from within my MVC application?
I have an application which lives at
http://localhost/myMVCapp/
and i put the asp files within a subfolder off the application root
i.e.
http://localhost/mymvcapp/asp/news.asp
I keep getting 500 errors if i try and request the file.
I believe i need to supply a web.config within this folder which allows the request through but i am unable to workout the correct settings.
I think you can Convert your folder to web application and then you should write your own Route.

ASP.NET MapPath to the root of the Solution

I'm having trouble selecting the root of the solution in my ASP.NET Application.
I have three applications inside this solution, a Web app, an API and an app for Reports. I was trying to select my Reports app from the Web app in code using Server.MapPath but I can't get to the folder.
I tried Server.MapPath("\Tagus.TMS.Reports/Media/VoyageControlReport.rpt") from inside the web app.
How can I get that app path?
The Server.MapPath will use the "application level of IIS" to determine the path returned.
So the path returned is the physcial location of the root of the application (web site) + what ever parameter you pushed into the "MapPath" method.
I would advise you to make a "/Data" directory in your website and use the "Server.MapPath("/Data/dataFileToLoad.rpt") to get the physcial path.
It also gives you a nice isolation for your data files (rpt) versus your runtime files (dll's).
Hope this helps,

Silverlight Cannot find XML data source

I am very new to Silverlight development. I understand that this is client side technology therefore the paradyme is differant from that of conventional ASP.NET development. Having said that, I don't understand where my server side code is deployed.
I have a silver light \ MVC application. I am trying to read an XML document from within my 'Models' folder. The following peice of code is executed from within a class that is in the same location as the XML document, 'Models'. The load() results in a SystemIOFileNotFound exception. I noticed that when building the application the XML document is not laid down in the same location as the web project's assembly. I assume this is specific to the fact that this is a Silverlight project. Can someone tell me what I'm missing?
_xdoc = new XDocument();
_xdoc = XDocument.Load(new Uri("videos.xml",UriKind.Relative).ToString());
Edit..
The behavior I am after is the start page (silverlight) populates controls via a server side controller. ie localhost/video
Silverlight can't access your filesystem (thankfully), which is why you can't access the file. Try embedding it as a resource, or storing it in the local storage API provided by silverlight.
Assuming that your Models folder is in the Web project (i.e. not the Silverlight project), I think that your problem is unrelated to Silverlight.
The code loading the XML file assumes that the file is in the current directory, so you need to ensure this through your deployment technique.
If you are doing this in the Silverlight part, you should put the XML file in an embedded resource and access it as a stream (get it with Assembly.GetManifestResourceStream) or as a resource (a la WPF, not an embedded resource) and access it with the package part syntax.
The problem was that I was attempting to access this static resource as you would in typical ASP.net. However I found it necessary to map the path to the file using the current HTTPContext:
HttpContext.Current.Server.MapPath("~/App_Data/videos.xml");
So the above worked for me. Since this code is in the web project and not in the silverlight project I am still unclear as to why I cannot just access this resource using a relative path. This code will be executed in the context of the web server.
i.e.
XDocument.load(../App_Data/videos.xml);

Resources