How To Configure IIS To Serve 404 Response With Custom Content
Mar 31, 2010
I would like to serve a custom 404 page from ASP.NET MVC. I have the route handler and all the infrastructure set up to ensure that nonexistent routes are handled by a single action:
public ActionResult Handle404()
{
Response.StatusCode = 404;
return View("NotFound");
}
Problem: IIS serves back its own content (some predefined message) when I set Response.StatusCode to 404 before returning the content.
On the VS development web server, this works as intended - the status code of the HTTP response is 404 while my content (the NotFound view) is served.
I believe that when the IIS processing pipeline sees that the application returns 404, it simply replaces the whole response with its own.
What setting in IIS affects this behavior?
I do not have access to the IIS installation so I can not investigate this - however, I can ask the hosting provider to tweak the configuration for me if I know what exactly needs to be changed.
Hoping there's been a "best practices" way to accomplish this so far. Basically, I'm building an ASP.NET MVC 3 site that I would like to host from a central database and server. I'd like for [domain1].com and [domain2].com to point back to this one server. Ideally, I'd like this server to see a request from domain 1, and serve content relevant to domain 1 (which is essentially a category of topical information -- the rest of the structure would be the same). I'd like folks not to get redirected away from that domain if possible.I'm thinking I should map each domain to a specific static IP, have all connections through those IPs connect to the central site, and return relevant data that way.What experiences have folks had doing this with the .NET stack, and are there any "best practices" to consider in this case?This might not be as clear as it could be; I'll aim to revise as I get questions.
As part of a CMS, I have created a custom VirtualPathProvider which is designed to serve a single file in place of an actual file structure. I have it set up such that if a file actually exists on the server, that file will be served. If the file does not exist, the virtual content stored for that address will be served instead. This is similar to the concept of serving a website from files stored in a database, though in this case the content is stored in XML files on the server.
This setup works perfectly when a request is made to a specific page. For example, if I ask for "www.mysite.com/foobar.aspx", the content that is stored for "foobar.aspx" will be served. Further, if I ask for "www.mysite.com/subdir/foobar.aspx", the appropriate content will also be served.
The problem is this: If I ask for something like "www.mysite.com/foobar", things begin to fall apart. If the directory exists on disk (and doesn't have a configured default page in IIS, such as index.aspx), I will get a "Directory Listing Denied" error. If the directory does not exist, I'll simply get a 404 - Resource Not Found.
I've tried several things, and so far nothing I've done has made a bit of difference. It seems as though IIS is simply noting the nonexistence of a directory (or default file in an existing directory) and serving up its own error code, without ever asking my application what to do with the request. If it ever did get to the application, I would be able to solve the problem, but as it stands, I'm quite lost. Does anyone know if there is some setting in IIS that is causing this?
I've looked for every resource I can find on the subject, and am coming up empty. I know this should be possible, because I have read tutorials on serving content from both databases and ZIP files.
Im looking to improve performance on my site and would like to implement OutputCaching for a number of actions on my controllers. The main problem is when administrators navigate to an action with an output cache specified I need to ignore the cached content and execute the action.
Is there a way I can configure the OutputCahing mechanism to not deliver cached content for specific users if they are authenticated and a member of a specific role?
I'm getting this message when going to a web app that accesses my service.
"The content type text/html of the response message does not match the content type of the binding (text/xml; charset=utf-8)..."
The thing is, it happens once in a while even when no changes have been made to the service or the web app. And I can make it go away most of the time by going directly to the .svc?wsdl page in my browser and then coming back to the web app.
I'm looking for a walkthrough on how to create and use a custom provider for ASP.Net Healthmonitoring.
So far I've only worked with the e-mail provider that generates e-mails on errors. Basically I want to do the same, but with more flexibility: I want to use the HealthMonitoring features (I don't want to use the Application_OnError event in the global.asax) in a way that allows me have access to an event, that gets thrown like "OnNewHealthMonitoringEntry" with all the information provided in the e-mail, to run custom code.
Edit: Based on the source code provided here [URL] I was able to build my own custom provider and implement it. Now I want to add some new attributes to configure my custom provider.
Is it possible to store configuration necessary for custom provider close to the healthMonitoring section? I guess I could include the settings into the appSettings node, but I'd like to configure it somehow with attributes (inside the healthMonitoring node). Is that possible?
As im new to this concept,i request anyone to explain in detail about configuring the custom handlers in IIS 5.1 STEP by STEP and also about mapping the extension.
How can I configure ASP.NET/IIS to show custom error pages (i.e. a nice friendly error page), but to still return a 404/500 status code so that google does not try to index the page? I have tried setting the Response.StatusCode to 404, but then the nice error page does not show anymore.
I try to mak a custom textbox. Something pretty easy to do but I can't figure out one thing. When I write my textbox in designmode I have this <custom:Textbox runat="server" ID="Textbox1">Test</custom:Textbox> The problem is The textbox is a webcontrol I added to my class [Parsechildren(false)] like this [Parsechildren(false)]
public class Textbox : WebControl { }
now the problem is I want to able to write the Text from the textbox between the beginningtag and andingtag but I want to disable every other control in designmode. When I add < between the tags then you can see you can write all tags between it. disable all controls except literal between the tage in designmode?
When "Configure ListView..." in the smart tag of ListView is clicked, one can choose one of the 4 styles on the list: Colorful, Professional, etc. I am wondering if there is any way to add one's own style to the list.
I have a button that when clicked on it will open a pdf file in the current window. Can someone help me modify my code to open in a new window? Here's what I have so far that works when the button is clicked:
I hope this is a ridiculously simple problem. In my ASP.NET MVC web application, I have several places where the appropriate response to the {whatever}/Detail/{#} URL is the binary content of a file that lives outside of the installed application, with an appropriate ContentType in the header.I don't_ want to expose the directory containing the files or its contents as files to the web. That would make it possible to browse other files by educated guessing and would require special work to interact well with the authorization mechanism.I _do_ want HTTP response to contain the content stream of the file corresponding to the supplied ID, so that it will result in the usual open or save-to-file stuff in the browser. The client should have no clue as to where the content originates I can imagine a solution where I write code in my controller to open streams and feed the output to the response, taking the HttpResponse by the throat and forcing it to skip the master page and forcing the content type. I'm already doing something similar to dump some log content into a support page.However, ASP.NET MVC is a strong framework and this has got to be a pretty common situation. Is there a kinder, gentler way to do this already provided by the platform, either through routing or in my controller? At first blush, I didn't see an ActionResult for this. I do see that returning something other than an ActionResult will cause the object supplied to be rendered as content, which sounds like what I want. However, what sort of object should I return to do this for a whole file, perhaps a relatively large.
How do I apply the response.redirect command to have the content of what I a url that I fetch displayed in my web app. I fetch the url and since vb.net is not recognizing response.redirect I am a little stumped. I have a textarea where I display the responsestream which works but I would like it displayed the same way it displays in a browser. The pages source has a lot of javascript functions so I am not sure if this is even relevant.
In order to support a legacy application that's in the field, I need my ASP.NET MVC app to return an empty response that also has a Content-Type. One of IIS, ASP.NET, or ASP.NET MVC is removing my Content-Type when I send back a null response. Is there any way around this? (While not requiring an empty response with a set Content-Type would obviously be the ideal solution, the clients are already out there, and many of them cannot be upgraded.)
EDIT: Since there was a request for code: I'm proxying the request from the new web application to the one that older clients rely on. To do this, I have a subclass of ActionResult, called LegacyResult, that you can simply return for those methods that need to be handled by the old software. This is the relevant part of its code:
public override void ExecuteResult(ControllerContext context) { using (var legacyResponse = GetLegacyResponse(context)) { var clientResponse = context.HttpContext.Response; clientResponse.Buffer = false; clientResponse.ContentType = legacyResponse.ContentType; /* Yes, I checked that legacyResponse.ContentType is never string.IsNullOrEmpty */ if (legacyResponse.ContentLength >= 0) clientResponse.AddHeader("Content-Length", legacyResponse.ContentLength.ToString()); var legacyInput = legacyResponse.GetResponseStream(); using (var clientOutput = clientResponse.OutputStream) { var rgb = new byte[32768]; cb; while ((cb = legacyInput.Read(rgb, 0, rgb.Length)) > 0) { clientOutput.Write(rgb, 0, cb); } clientOutput.Flush(); } } }
If legacyInput has data, then Content-Type is set appropriately. Otherwise, it's not. I can actually kluge the old backend to send an empty v. non-empty response for exactly the same request, and observe the difference in Fiddler.
EDIT 2: Poking around with Reflector reveals that, if headers have not been written at the time that HttpResponse.Flush is called, then Flush writes out the headers itself. The problem is that it only writes out a tiny subset of the headers. One of the missing ones is Content-Type. So it seems that, if I can force headers out to the stream, I can avoid this problem.
How can I remove the html content and add new string to the Response object. If I use the Respose.Write method the page contains the string, but while taking the view source option from browser it will display some html tag like Doctype, head, body etc. My requirement is only the string should be displayed in the source.
I have an httpmodule and it has a handler for OnEndRequest. I am trying to write the HttpContext.Response.OutputStream to a file. I am trying to use the Read method of it. But when it is called the exception message i get is "Specified method is not supported". So i am not able to do what i want that is, write the response to a file on the disk. I am able to write the HttpContext.Request.InputStream to a file using its Read method which i do in a handler for OnBeginRequest.
I'm trying to query a database that contains information about a "ticket" using jQuery's .ajax() method.
$.ajax({ type: 'GET', url: 'Preview.ashx', data: 'ticketID=' + ticketID, success: function (data) { // 'data' should be a row from the database, so it should be like an // array that contains each column of the row // do stuff with this data } }); ... so that all works fine. I'm having trouble with the data variable. On the server side, I do... // get the ticket ID from the POST parameter int ticketID = context.Request["ticketID"] != null ? Convert.ToInt32(context.Request["ticketID"]) : -1; if (ticketID >= 0) { // grab the data from the database, getInfo() will retrieve the row // in the DB that corresponds to the ticket ID given, returning an // ArrayList with all of the information ArrayList theTicket = getInfo(context, ticketID); // now, I need to somehow return this information so that I could deal with it // in the 'success' callback function above return; } else { // something went wrong with the 'newTicket' POST parameter context.Response.ContentType = "text/plain"; context.Response.Write("Error with 'ticketID' POST parameter. "); return; } return;
I've debugged this enough to be sure that the ArrayList contains the correct information. Now I just need to return it. How would I do this? How would I return the data in the ArrayList? Is it possible to structure the response so that I could do data.ID, data.otherColumnName, etc... in the callback function to access the different fields?
On the client side I have a form: <form ... action="www.link-to-another-domaint" > <input type="hidden" id="asd" value="fgh" > .... </form> <script type="text/javascript"> document.forms[0].submit(); </script>
Then on the other domain - there is also my other application - I'm trying to get the hedaer "key" by this code:
Request.Headers["key"].ToString();
But there is no such header. Is there is a desicion? Where is my mistake?
In IIS 7.0 integrated mode after deleting all headers with Response.ClearHeaders() IIS would add some other headers like Server and X-Powered-By which reveals good information to hackers. How can I stop this behavior (consider I still need to add my custom headers) ?