Visual Web Developer (Express): Setting Document Root for Dev Environment - c#

I'm developing a site in Visual Web Dev Express, and when I run/debug, I'd like to be able to set my application's document root so that I can use safer paths, like "/css/style.css' instead of "css/style.css". How would I accomplish this?

Click on the web site node in the solution explorer.
Press F4 to see the properties window.
Change the virtual path from /projectname to /
Bear in mind that this has an impact on how you expect the application/web site to be deployed. If it is ever used outside the root of a web server, the URL paths will be incorrect.

I have been looking for the virtual path field in the properties window but haven't been able to find it... it only shows me the following options
aperture directory URL
complete access route
policy file
port
use dynamic port
Do you know of some place else where I could find the Virtual Path field?

Select the "Web" tab and it's in the Servers section

Related

Environment.ExpandEnvironmentVariables returns unexpected path in IIS

var filename = Environment.ExpandEnvironmentVariables(#"%LOCALAPPDATA%\Dropbox\info.json");
When I run it in vs I get the path I need which is
C:\Users\User-pc\AppData\Local\Dropbox\info.json
when published to IIS I get
C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\Dropbox\info.json
This is because IIS is not running under your user account. It is running under a system account. C:\WINDOWS\system32\config\systemprofile\AppData\Roaming\ is the correct path for the IIS account.
You could set the user account on the IIS app pool to be your user (not great for secuirty, https://technet.microsoft.com/en-us/library/cc771170(v=ws.10).aspx), or you could pass the correct path in another way (web.config maybe)?
%LOCALAPPDATA% is a user specific folder.
IIS doesn't use the same user as you when you run from VS.
In my opinion there are 2 solutions :
- Change the folder of this file to something more usual (Your web site folder, or something like D:\Dropbox\info.json)
- Change the user IIS uses, but that's really not a good option

Changing virtual path and enabling/disabling sites in code

I am working on a web project that contains three web services and a website. My source code is stored in TFS for version control and I regularly have to work in several different branches on the same code. These branches are merged regularly and are all set up in the same way. They all use WebDev.WebServer as default host. Which is fine because it saves other teammembers from having to continuously alter settings in IIS to switch to the proper folder.
Well, guess what?
I want to move to IIS and still be able to use the same config for every branch. This will results in conflicts since I Need to change the virtual folders for these projects every time when I switch branches. I also need to be able to turn off these IIS sites to enable the use of WebDev. Manually, that's a bit of work and I'm a programmer. This needs to be automated...
My idea is to create a small web application running on my local host, which I can use to change the virtual folder and/or to turn on/off the IIS site. Sounds simple enough so all I need are two things:
How do I change the virtual folder of an IIS site from one location to another in code?
How to turn on and off a site in IIS?
Simple, right? So, point three: do you have any other suggestions that I could use?
Oh, other developers are also working on the same project. Not all of them will use IIS to run it. Thus I cannot alter the config files for these projects. I must manage it all through IIS.
Personally, I would use Powershell in this instance.
$websitePath = "IIS:\\Sites\Path to your website in iis"
$fullWebSiteFilePath = "file path to your content"
if(-not (test-path $websitePath))
{
new-item $websitePath -physicalPath $fullWebSiteFilePath -type Application
}
else
{
Set-ItemProperty $websitePath -name physicalPath -value $fullWebSiteFilePath
}
with a little jigerry pokery you could read the different configurations from an xml file and then call the shell script passing the xml file name as a parameter.
To manage IIS7 programmatically, you can start with the ServerManager class.

Change Browser download folder using C#

Is there a way I could change the download folder of the default web browser using c#.
Concurring with other's comments, you can only do it in a desktop app if you have the right permissions.
Here's some sample code to find out the default browser of the system (from this post):
private string getDefaultBrowser()
{
string browser = string.Empty;
RegistryKey key = null;
try
{
key = Registry.ClassesRoot.OpenSubKey(#"HTTP\shell\open\command", false);
//trim off quotes
browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!browser.EndsWith("exe"))
{
//get rid of everything after the ".exe"
browser = browser.Substring(0, browser.LastIndexOf(".exe")+4);
}
}
finally
{
if (key != null) key.Close();
}
return browser;
}
However, things get tricky from here. Different browsers have different ways of saving the default location.
E.g.,
IE may store it in registry (usually under HKEY_CURRENT_USER\ Software\ Microsoft\ Internet Explorer)
FF stores it in prefs.js in Profile folder (checkout this post to get to it via code)
Not sure about Chrome and Safari
but you get the idea.
Not sure what your end goal is, but from a UX standpoint, I think the best thing to do would be to ask user to specify the Download directory (in other words, you expose a Setting in your App for the default download location).
To expand on Ash's comment - if you're within a web app, no. If you're a desktop app, and you have sufficient permissions (i.e. running as Administrator), probably. But you'd need to find the default browser (from the registry presumably) and know how to set the download folder for each popular browser, or every browser you want to work with.
Where are you trying to do this from? If you mean "someone hits our website and ...", the answer is no, as anything you run is in a security context. You can certainly suggest the user changes the folder, but you are stuck.
Assuming you are not a web application, you have options. The main user download directory is located at X under the key {374DE290-123F-4565-9164-39C4925E467B}. Yeah, that sounds like a lot of fun. You can learn how to hack the registry programatically here. But, the user can specify a specific folder in the browser, as well. This means you have to know what browser the user is using and hack it, or you can attempt to hack all.
The bad news is the app, running (most likely) in the user context, may not have administrator rights and be able to whack the registry keys to change the folder.

Server Root and MapPath()

I have a file structure set up like this:
ServerRoot
applicationRoot
filePage.aspx
files
chart.png
My application page called filePage.aspx uses another app to custom build charts. I need them saved in files folder. This is how our client's production server is set up and I cannot change this.
I do a _page.Server.MapPath("/files") but it gives me a InvalidOperationException and states Failed to map the path '/files'.
UPDATE:
So it has to be set up this way MapPath("/"). My local asp.net server can't handle the MapPath that way, but our IIS development box has no problem with it and it works fine. Interesting.
How do I get it to save to files?
I believe it's a security violation to go outside the directory structure of the virtual directory in asp.net 2.0 and up. You'll need to make a virtual directory to the directory and use that.
Use
Server.MapPath("~/files")
The ~ represents the root of the web application so the folder returned will be correct no matter which subdirectory you are in.
Try
_page.Server.MapPath("files")
_page.Server.MapPath() will attempt to map from the root of the application (NOT the root of the server).
Try _page.Server.MapPath("../files").
EDIT
You may run into security issues when trying to map outside of your application root. If that is the case, you can do something like this:
Server.MapPath("~").Substring(0, Server.MapPath("~").IndexOf(VirtualPathUtility.ToAbsolute("~").Replace("/", "\"))) + "\files"
This looks rather complex, but essentially says "map my application, then remove the application root from the end and add '\files' instead".

ITextSharp saying File not found when published to My server

I am using ItextSharp and I am getting a new font for it by doing this
string fontPath = Path.GetFullPath("C:\\BMSApplicationFiles\\Fonts\\FREE3OF9.TTF");
BaseFont barCodeBase = BaseFont.CreateFont(fontPath, BaseFont.CP1252, BaseFont.EMBEDDED);
That file is in that directory on both my dev machine and the server.
When i run this on my dev machine it works fine, when published to my server I get an error saying FREE3OF9.TTF is not found as a file or a resource.
I have double and triple checked that the file is in fact there on my server. My application has full trust on IIS.
and IIS user has full access to this folder. And still I get this error. Very frustrating.
Any ideas??
This code is Not inside the Web Application but inside a DLL class library that the web application is referencing. Do I need to add any more special permissions or something?
Your next step should be to confirm that what you think is happening is happening. If you are allowed to, download and install Process Monitor which will allow you to log activity including which files are being attempted to be accessed and by which user account. Apply filters when monitoring of course, or else you'll end up seeing all the activity on the server...
If that only confirms that the correct file is being attempted to be read by expected user then maybe there's something up with the font. Try replacing the font file temporarily with another standard one (e.g. arial or something) and see what happens. (this is unlikely to help as it works on your dev machine, but might be worth a go)
Most probably you have a security issue.
If you run IIS7 then check that IIS_IUSRS group has an access to the file (open file properties in explorer select Security tab etc).
If you run IIS6 then check that IUSR_MachineName account has an access to the file.
I had a similar error when I tried to get it working with IIS. Within IIS, I had to add he file directory as a virtual drive. That worked for me when I had a similar situation set up. Hopefully it works for you, best of luck

Categories