How to serve non-aspx files from a custom ASP.Net server - c#

I have a custom ASP.Net server in a WinForms application. The server was created using CreateApplicationHost and a simple HttpWorkerRequest implementation.
I find that the custom server only processes requests for aspx files. If I try to access xml / txt / png files from the browser, it gives a "The resource cannot be found." error.
My question is: what must be done to be able to serve such files?

The answer is that the HttpWorkerRequest.SendResponseFromFile method must be overridden to send the file via the response stream.

Related

nancyfx unreachable exist file

I use nancyfx.
I have published a folder with files.
I start the server and observe the following situation:
If there is a file '1.html' in the published folder, then I get it through the browser. If I delete this file from the disk, I get an error 404 (this is correct). If I add this file to disk again, or I change its contents, I normally get it in the browser.
If after starting the server I try to access from the browser to a nonexistent file '2.html', I get an error 404 (This is correct). However, if after that I create the file '2.html' on the disk, I still get a 404 error. It helps only restart the server.
I got the impression that nancyfx at the first access to the requested files forms some kind of cache, which subsequently does not allow me to get the files added after they were unsuccessfully requested.
Help please with the decision of the given problem. Thank you in advance.
Nancy uses a convention based approach for figuring out what static content it is able to serve at runtime. you can check the Documentation here

Getting the content of a .aspx file in a .cs class

What's a good method for getting the content of a .aspx web page as a String. This is from a C# Class which is compiled and deployed to the same machine.
Should I use a HTTP request? Or is there a means of doing the same through a file path, and would this trigger the code behind of the page?
If you use HTTP request, like WebClient and use WebClient.DownloadString(#"http://someSite/somepage.aspx"), that would trigger the server side code and you will get HTML generated by the server. Not the actual aspx page.
But if you use File.ReadAllLines('somepage.aspx') from your current project then you will get the file contents and it will not trigger the server side code. But you can only do that from your current project. You can't access an aspx page over http
This code retrieves the root directory that is hosting the application in IIS. Then, it concatenates the filename. Finally, it reads the contents into a string variable.
Reading a file from disk does not trigger the code-behind; only a request through the ASP.NET pipeline does that.
string path = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "Index.aspx");
string fileContents = File.ReadAllText(path);
An HTTP request would usually not return the content of the aspx file since the server should interpret it (take this with a grain of salt, it all depends on your server and how its configured, maybe you've got a special setup)
If you want to read the content of the aspx file, open the file using its location on the disk. It won't trigger the code behind the page since this is triggered by the web server and you're not using it using a file path
if you want to read the output produced when the aspx file is read, open the url to this file. It will trigger the backing code and you will received the output.

Use HTML string from Server Requets, and create the web page without saving it a file [in C#]

I´m sending the value of a variable via POST to a PHP page in C#. I get the data stream from the server that has all the web page in HTML with the value of the POST. This information is stored in a string variable.
I would like to open a browser and show the web page (maybe using System.Diagnostics.Process.Start("URL")), without having to save it in a file, this is showing the page in the moment and, when the browser is closed, no file is stored in the server.
Any idea?
Drop a WebBrowser control into a new form webBrowser1 and set its DocumentTextProperty to your result html
webBrowser1.DocumentText = ("<html><body>hello world</body></html>");
source:
<html><body>hello world</body></html>
You aren't going to be able to do that in an agnostic way.
If you simply wanted to open the URL in a browser, then using the Process class would work.
Unfortunately, in your case, you already have the content from creating the POST to the server, and you really want to stream that response in your application to the browser.
It's possible among the some browsers, but it's not able to be done in an agnostic way (and it's complicated even when targeting a specific browser).
To complicate matters, you want the browser to believe that the stream you are sending it is really coming from the server, when in reality, it's not.
I believe that your best bet would be to save the response to the file system in a temp file. However, before you do, add the <base> tag to the file with the URL that the file came from. This way, relative URLs will resolve correctly when rendered in the browser.
Then, just open the temporary file in the browser using the Process class.

Creating a Dynamic iCalendar Feed

I am looking for a means to generate a iCalendar feed using .Net , atm I am using the DDay library to generate the iCal file and write it to the response.
The problem with this is if I feed the URL to outlook it says that it is a invalid ics file.
I have tried various routes including :
Using MVC routing so that the link contains a .ics extension.
Changing the Mime Type so that it is Text/Calendar.
One observation I have found is that if I navigate to the URL in a browser a valid iCal file is generated and is imported into Outlook correctly. Secondly if I take that exactly file and host it via IIS as static content , Outlook picks it up as a valid file.
The problem is that I need to generate a Dynamic feed and seem to have run out of things to try and get this working.
It seems the problem in my case was that the default asp.net auth was blocking outlook because it was not authenticating , once a exception was added to the web.config , the URL loaded correctly in outlook

Forward file upload details to another remote service

I have a service for uploading files which works well.
Is it possible to submit a file upload to a asp.net method (like normal uploads), but then forward the upload file details to the remote service. Therefore using the asp.net method as a proxy and NOT actual upload method?
The actual file saving will be done at the remote service.
Note
I'm using c# and .net 3.5
Regards
If you are using an ASP.NET method, the file MUST be uploaded to the server. However, it doesn't have to be saved using the "SaveAs" method or any other method. You can access the file directly as a stream which you can pass to your other service if you can send streams to it.
The idea is explained in this blog post (slightly different use but same idea):
http://weblogs.asp.net/meligy/archive/2008/02/18/unit-test-friendly-file-upload-handling-in-n-tier-applications.aspx
So, if your remote service call can be simplified as a method like:
public void MyServiceMethod(Stream inputStream) { ........ }
You can pass the file content from the page without saving it some way like:
myService.MyServiceMethod(myFileUploadControl.PostedFile.InputStream);

Categories