How can I make a server log file available via my ASP.NET MVC website? - asp.net-mvc

I have an ASP.NET MVC website that works in tandem with a Windows Service that processes file uploads. For easy maintenance of the site, I'd like the log file for the Windows Service to be accessible (to me, only) via the website, so that I can hit http://myserver/logs/myservice to view the contents of the log file. How can I do that?
At a guess, I could either have the service write its log file in a "Logs" folder at the top level of the site, or I could leave it where it is and set up a virtual directory to point to it. Which of these is better - or is there another, better way?
Wherever the file is stored, I can see that there's going to be another problem. I tried out the first option (Logs folder in my website), but when I try to access the file via HTTP I get an error:
The process cannot access the file 'foo' because it is being used by another process.
Now, I know from experience that my service keeps the file locked for writing while it's running, but that I can still open the file in Notepad to view the current contents. (I'm surprised that IIS insists on write access, if that's what's happening).
How can I get around that? Do I really have to write a handler to read the file and serve it to the browser myself? Or can I fix this with configuration or somesuch?
PS. I'm using IIS7 if that helps.

Unfortunately I'm afraid you'll have to write a handler that will open the file, and return it to the client.
I've written an IIS Manager extension that displays server log files, and what I've noticed that even the simple
System.IO.File.OpenRead("")
can still run in the same problem, and return the same error.. It was kind of confusing.
In the end I used
System.IO.File.Open("", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
and I could easily open the file while the server was writing logs to it :)

I think the virtual directory is an "okay" solution, if you add the directory (application) with READ ONLY rights + perhaps "BROWSE directory" too (so you can see the folder contents rendered by the IIS).
(But once you do that, you should consider that you also anonymous access to that folder - unless you enable authentication, so watch out for "secret" contents of the logfiles that you might expose? just a thought.)
Another approach, I prefer myself, is to make a MVC/ASP.NET page that does the lookup in the folder by normal code, so that you 100% can filter whatever data is shown in the HTML.
You can open the files as TextStream's and in Read Only mode.
If it's a problem to gain access to the logfolder, I would use the virtual directory with READ ONLY access and then program something that renders the logfiles as HTML on my screen and with my detail levels. Perhaps even add some sort of "login" first. But it all depends on your security levels and contents of logfiles.
is this meaningfull to you? if not, please explain more, as I've been through this thought a few times already for similar situations.

Related

how can i know the physical path from selected file (fileupload bootstrap)

I´m making a web application in MVC, I have a view where I select a file from my pc directory (like opendialog form in windows applications), bootstrap´s fileupload gets the file name but I want to know his physical path to. Because I want to save it in my data base (where file come from).
Anybody can help me??
Thank you in advance!
You can't, this information is never sent to the server for obvious security reasons. Only the filename is sent to the server. So you cannot store the physical path where the file originated from the client machine on your server.
When you upload a file via a web browser, the actual file path is not supplied. This is seen as being a security feature.
There is no way to circumvent this using pure HTML. Some people get around it by using a plugin such as Flash or Silverlight to upload the file, but I recommend living with this feature if you can.

System.IO.IOException destroys session after RedirectToAction [duplicate]

I have a web app where the administrator can create news, pdf documents and other stuff in his cms panel.
The problem is when the admin delete a new or something else the app deletes all the files related to that new, I mean the images, pdfs and other documents. Tha main problem is those files are stored in folders under the "news" folder and when the app deletes them the session is lost.
How can I do to have a file system without losing the session?
I'd like that file system within the app folder...
Impossible for us to store those folders outside the app and we don't want to use StateServer because of the performanne....
Any other solution?
Thanks
Your session is lost becasue IIS recompiles. The easiest solution in my opinion is to store your files outside the wwwroot.
Discussed on SO: ASP.NET restarts when a folder is created, renamed or deleted
[Update]
Example:
Let's stay your app is in c:\inetpub\wwwoot\virtualdir1
You make a work directory:
c:\inetpub\inetwork
Give the proper rights (read/write/etc) to the Asp.net user of your app pool and it should all work like a charm.
More info on setting the rights: What are all the user accounts for IIS/ASP.NET and how do they differ?
Store the path to the workdirectory in your web.config (you no not want to hardcode it)
Having those files within the app folder is a poor desgin. The session is probably lost as you are causing IIS to recycle due to the file system changes. It is much safer to not have your web application able to write to its own folder, doing so is a security risk.
Separate your document folder and web site folder. And give right permission your document folder.

open an excel file located on the server instead of download it MVC

I'm building a Reporting web application right now with MVC3 and I've come up to a couple problems.
My goal is to have it able to generate and view Crystal Reports, SSRS reports, and Excel documents.
Right now I'm working on the Excel segment and I'm running into more trouble than I thought I would. First off, when I link directly to the file, it either opens inside the browser or it downloads it from the server and if the user makes changes it doesn't actually save it to the true file on the server.
I've tried both linking to the file directly using Razor and a ViewModel with the path to the document as well as directing it to an action that returned a File.
I've also tried linking it to a shortcut to the actual file thinking that if I could open the shortcut it would open the file the way I wanted it to and unfortunately it didn't really open at all.
The users already have access to the files on the server through a network drive, so as of right now they can go into the server, open the excel document, edit and save it no problem. I want to duplicate this effect through a link. The program already has a file browser built, so I can browse between the files and make links to the reports.
Thanks in advance!
Since they are apparently on a network drive, you can just link to the files directly, relative to the user?
For example: a link to file://///SERVERNAME/folder/
I tested it between two computers on the network, and that seems to work. However, you still get a popup asking that you want to do with the file, open or save. (both in firefox and IE)
Note: Yes, that many slashes seem necessary, lol

Open a file that is on a file server from a webpage?

I am working on an internal application. We have a website that displays all our SSRS reports for a group of work. I have been asked to see if I can link all the files (pdf, word, excel) for the group of work. These files are stored on a file server that users viewing the reports have access to. Each group has its own group of reports and shared files.
Is it possible to open the files (without downloading them) from a webpage? Meaning that they file is opened from the file server? I don't want people to download a copy of the file.
I am pretty sure this can work with IE because sharepoint does it. However, other browsers may have an issue.
EDIT: What I would like is to have a web page with links to the files. When they click on a link (say for a word doc), word will open the file that resides on the file server. Without out a local copy downloaded from the network share.
EDIT2: Please note, I know what I am asking is probably not possible in all browsers. I am more or less just making sure. It seems possible in IE using activeX, but out side of that browsers do a good job at keeping processes inside a sandbox.
3 options. Remember this is for an internal website.
link to the share using file://. This will have the side affect of downloading the file to be viewed. As long as user clicks open every time it should not be a big deal.
Use JavaScript and activeX to open word (excel, reader, ect) passing in the file path as a command line arguments. This works only in IE and in win7 (probably vista) user will get a pop up asking if it is ok for the activeX control to run.
Create a new protocol. openfile://. This would be set up to run an application that is installed on the client machine which would open the file. Since it is internal, the application could be installed on the machines without issues. This also requires a registry change.
I haven't picked one as this change is still being looked into but i figure I would update this in case someone runs into something similar.

IHttpModule not being applied to virtual directory

I have a network folder that is mapped to my iis app as a virtual directory and I'm trying to do some authentication for files that are located there with an ihttpmodule. I've verified that the ihttpmodule is firing properly for anything else in my app, just not the files located in virtual directory. Most of what I've found is that the directory can't be listed as an application (which it isn't), and everything should work. The other solution that I found was to add the the module tag to the tag, but that didn't seem to help either. Everything that I've found talks about stopping this from happening. So my question is what could be set that is causing this to not work? Is there a certain execute permission that needs to be set? Any other iis settings that could cause this? It is an mvc app, and this is how my directory structure is laid out:
server/app <- my application folder
server/app/content/downloads <- downloads is the virtual directory
Do I have to add the virtual directory directly under my app directory? Is that part of the problem? I don't have direct control of the server my code is running on, so testing things out is a bit of a pain... so I was looking for some more thoughts before starting to send emails off to my operations people. Thanks!
Well, I think I found what the issue is.... it looks like by default, iis6 does not pass executables, zip files, or anything of that nature through the .net runtime. It only does that with .aspx files and such, so it will never fire my ihttpmodule. It looks like you can force it to by adding a mapping for that extension, but is there a code work around for this? Or is that the only way?

Resources