Writing a PDF outside ASP.NET MVC website folder - asp.net

I am trying to write a PDF file outside the ASP.NET MVC website folder and I get the following error:
Cannot using a leading .. to exit above top directory
We have another dataset static website that need not to be affected from website modifications and accidentally deleted. I suppose that the problem is the following line of code:
string path = Server.MapPath("../../Data/Invoices");
How can I workaround this limitation? I thought about disabling the Web Deploy option "Remove additional files a destination" but it's to risky, we need to maintain invoice copies for years. I'm scare from new DEV machine installations/new programmers, etc. We are working on Windows Server 2008 R2 and IIS 7.
Any other creative idea is welcome.
Thanks.

Even though the idea of keeping data on the application server makes me uncomfortable; you can use a virtual directory that points to a location like; c:/Alberto/Data/Invoices and map your virtual directory from your application to http://{server}/albertos/invoices (invoices would be your virtual directory and albertos would be the application name). Then I think you can call Server.MapPath('~/invoices') on it as you wish.

Related

How can I get the root of my project in ASP.NET and not the location of IIS Express using .CurrentDirectory()?

I have an ASP.NET Core project that I'm developing and I'm making use of LiteDB as a database solution. In order to instantiate my database I need to provide a URI so that the program knows where to create the database.
Usually I'd do something like System.Environment.CurrentDirectory() to find the current directory I'm in and modify that, however the result of this command turns out to be C:\program files\IIS Express or something similar. Basically the current directory points to the location of IIS Express.
Instead I want to get the root of my ASP.NET project, the location that contains my controllers folder, appsettings.json, bin and obj folders.
What command do I need to use to get a string representing this location? I don't want to use a hard coded string for obvious reasons.
If you don't have access to DI you could try:
Assembly.GetExecutingAssembly().Location
If you have access to DI, try this:
Inject the IHostingEnvironment and call environment.ContentRootPath
How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac
You may be better served by using a dedicated location (such as a network share) rather than the folder that contains the executable code. Having user data mixed into deployed code comes with a lot of headaches:
Deployments become more complex as they need to account for the presence of user data. This is especially true if you need to delete pre-existing code during a deployment.
If the user data is sensitive, developers may be denied access to read the deployed code. This can make troubleshooting issues much harder.
Backups of deployed code will contain user data, which means they will always appear to be different even if nothing (code-wise) has changed.
Of course, this all assumes that the production environment is configured differently from developers' local machines.

Automatically correct .vs/config/applicationhost.config

Visual Studio 2015 has moved the IIS Express configuration file from My Documents into its own hidden .vs directory and tells IISExpress.exe to use that configuration file.
While this may sound like a good idea when you have multiple web applications with conflicting configurations, in fact it's not when you have web applications with custom configurations at all. Because this config file is in a temporary directory which can be deleted when checking out code into a new location from source control, or when cleaning up things in general.
The old location in the Documents folder is a central location and files there usually won't be deleted. If I need to make some changes to this file, like allowing the application to use Windows authentication, I edit this file once and I'm done. Now with the new location, I may need to make these changes multiple times. And since there's two config files (the old location still exists for me), it's even more confusing.
Then you shouldn't commit anything from .vs into version control, so it's not even possible to share these customisations with other team members.
What is the recommended way to update the applicationhost.config file so that it provides the environment required by the web application? Is there a tool that can run in a pre/post build step? Is there a customisation XML file that can be merged into the default file from .vs?
Is it possible at all to just check out a web application from source control and let it work in a local IIS Express? Or will it always require manual corrections after getting the code and web.config?
PS: I've forgotten one thing: There's the <UseGlobalApplicationHostFile> element in the project file which can be set to true, but as soon as I do that, I get an access denied error message every time I want to start the web application. So that doesn't seem to work well.

Asp.net writing server side file

Needed to write a server text file as the output of a business process initiated by ASP.net app.
The text file writing code is in a library file using standard stream code
All worked OK in IDE.
Publish and it falls over trying to write file. IIS is reluctant to write to the file system.
Much rummaging around and hair pulling finally led to a solution. It is not pretty and only applicable in a situation where you have control over the Webserver.
Just saw your answer.
It doesn't need to be inside your inetpub or wwwroot directory for that matter, it could be anywhere, as long as security permissions are set correctly for the user under which the application is running as.
But this is actually desired. If not just imagine the consequences of allowing write access anywhere.
Also, there's no need for the virtual directory. You could create a directory like C:\ProcessOutput, and grant permissions accordingly and it should work just fine.
Another option, would be to have a service account created, and impersonate as that user within your application only for when you need to write that output file.
Solution was:
Create a physical directory on the webserver with the physical path of:
c:\inetpub\wwwroot\mywebapp\myOutputFileDirectory
Make a virtual directory that points at the directory.
Using windows explorer give write permission to the physical directory to IIS_IUSRS.
Use a physical path of c:\inetpub\wwwroot\mywebapp\myOutputFileDirectory in your Streamwriter code
Maybe the virtual path could point to somewhere more sensible across the LAN if you get the security sorted but I am sufficiently battered to accept this small crumb with gratitude.

How to access files on the windows server 2003 programmatically

I'm trying to build logviewer for logfiles stored on our server.I know about the logviewers available on codeplex. But I'm looking to build something of my own.
Here's what I need to do first and foremost. The log files are stored on server with address \windows server address\LogFiles. Over here there are various folders with names W3SVC69773105,W3SVC1208646085. Now I can't make head and shoulders of this folder name but each folder represents an application. Inside each folder there are log files for each day.
I need to be able to get the latest file from the server, and then do parsing on it. I want to know how can I access this file located on server programmatically from my local machine.
Insofar as the folder names, it is actually pretty easy to generate them presuming you can access the IIS api. You see, W3CSVC4572 maps to IIS site ID #4572. So, you should have a log folder for each IIS site ID in a rather predictable pattern.
I'll resist my urge to ask why you are in fact reinventing the wheel. Except log parsing is alot less sexy a technology than wheels . . .

Cannot execute System.IO.File.Move to a network drive from SharePoint Server 2007 (MOSS)

I am using VS2008 C# and MOSS (SharePoint Server 2007).
I have created an asp.net web form which appears on a WebPart within a SharePoint site. When submitting the form, a small .csv file is generated. Ideally I want to have this file created on a network drive (on another server), but for some reason I cannot do this. I can create the file happily on the hard drive of my SharePoint server itself that I am working on, but it just never appears on any network drive that I choose.
I then thought I'd create the file on my c: drive first (as it works), then use asp.net to 'Move' the file to the network directory. I used:
System.IO.File.Move(sourceFile, destinationFile);
This failed also at the 'moving' stage. - the file gets created fine! On the MOSS server I am working on, there is a C: drive and a D: drive (partitioned). The file creation works fine on both drives, but just not any network drive even if I avoid using drive mappings as below:
(e.g. "G:\\Group Files\\" or "\\\\Global\\Group Files\\" )
Obviously, I thought security was an issue, so I ensured the MOSS server and the network server both allowed each other with 'Full Access' using Active Directory. I even granted access to myself as a user, admin groups, the ASPNET account, NETWORK SERVICE account (amongst others), etc with MODIFY access. Still no joy. I can PING the network server that I want to create the .csv file on, so it is 'seeing' it.
The work-around that I have done is create the file on the SharePoint server's c: drive, then run a batch file (on schedule) that purely copies the file to the destination G: drive - this works a treat, but I am frustrated that I cannot create the file on the destination server straight away, using code.
I've got a hunch it is SharePoint related, but if anyone can shed light on this matter I'd be extremely grateful!!
Thanks in advance, Ash ;-)
Ah, I had this problem today and found this thread whilst looking for an answer. My problem was that my temp directory didn't have the right permissions! Sure, I could get the file uploaded without issue, but I couldn't move it unless I were using a LAN account. I made sure NETWORK SERVICE and my sharepoint application pool had access, and made sure the user who was uploading the file had permission too. Then it worked.

Resources