How To Throw 404 Error Asp.net
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How can I return a 404 error from an asp.net handler? up vote 7 down vote favorite 1 I have created a handler for downloading a file. I want to return a 404 error if the file does not exists or user does not have rights to download that particular file. Is it possible? If yes, how? Sample code would be appreciated. c# asp.net error-handling share|improve this question edited Dec 31 '12 at 16:02 Randy Levy 19.1k44275 asked Dec 31 '12 at 5:26 Bilal Fazlani 2,53322345 2 What you have tried? –gks Dec 31 '12 at 5:28 4 context.Response.StatusCode = 404; ?? –Mitch Wheat Dec 31 '12 at 5:28 3 hanselman.com/blog/ABoilerplateHttpHandler.aspx –Randy Levy Dec 31 '12 at 5:29 itzonesl.blogspot.com/2012/12/get-files-in-directory.html –chamara Dec 31 '12 at 5:44 add a comment| 1 Answer 1 active oldest votes up vote 19 down vote accepted Check this code public class MyHandler : IHttpHandler { private const string CONSTSOMEPARAM = "SomeParam"; public MyHandler() { } public void ProcessRequest(HttpContext context) { // Don't allow this response to be cached by the browser. // Note, you MIGHT want to allow it to be cached, depending on what you're doing. context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetNoStore(); context.Response.Cache.SetExpires(DateTime.MinValue); if (ValidateParameters(context) == false) { //Internal Server
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Correctly send user to 404 if dynamic content is not found (ASP.NET MVC) up vote 3 down vote favorite 2 I have implemented 404 handling for http://stackoverflow.com/questions/14097785/how-can-i-return-a-404-error-from-an-asp-net-handler the general case in ASP.NET MVC 3, for when a controller/view is not found. But how should it be handled inside the controller if the user is trying to access something that can't be found? For example www.foo.bar/Games/Details/randomjunk will call this inside GamesController: public ActionResult Details(string id) // id is 'randomjunk' { if(DoesGameExist(id) == false) // Now what? I could just do a return Redirect('/Errors/Http404'); but that doesn't seem like the correct way to do it. http://stackoverflow.com/questions/10163238/correctly-send-user-to-404-if-dynamic-content-is-not-found-asp-net-mvc Should you throw an exception, or something else? We could have a special view in this case, but to start with we need a good way we can apply to several cases. Edit: I want to show my friendly 404 page I already have for the general case. c# asp.net-mvc asp.net-mvc-3 http-status-code-404 share|improve this question edited Apr 15 '12 at 15:17 asked Apr 15 '12 at 15:11 John-Philip 343419 add a comment| 3 Answers 3 active oldest votes up vote 7 down vote accepted You should throw HttpException 404 : throw new HttpException(404, "Page not Found"); share|improve this answer answered Apr 15 '12 at 15:21 Antonio Bakula 15.4k44568 This does exactly what I want! The user ends up at '/Errors/Http404?aspxerrorpath=/Games/Details/randomjunk', sees a nice page, and the error is logged on the server. –John-Philip Apr 15 '12 at 15:31 add a comment| up vote 4 down vote EDIT: Apparently per Darin Dimitrov, what I had before doesn't work even with customErrors. As Antonio Bakula says in the other answer, you have to do: throw new HttpException(404, "Not found") Then the customErrors will work. There's a built-in helper method called HttpNotFound so you can just do: return HttpNotFound(); You could also explicitly return a 404 with HttpStatusCodeResult: return new HttpStatusCodeResult(404); HttpStatusCodeResult is helpful when there's not a specific helper method or class for the code yo
ASP.NET Community Standup Forums Help Home/ASP.NET Forums/General ASP.NET/Web Forms/Throwing 404 error Throwing 404 error [Answered]RSS 7 replies Last post Oct 07, 2009 05:28 PM by pencho ‹ Previous Thread|Next Thread http://forums.asp.net/t/1448958.aspx?Throwing+404+error › Print Share Twitter Facebook Email Shortcuts Active Threads Unanswered Threads Unresolved Threads Support Options Advanced Search Related Links GuidanceSamplesVideos Reply msfit None 0 Points 9 Posts Throwing 404 error Jul 19, 2009 https://msdn.microsoft.com/en-us/library/bb397417.aspx 02:34 PM|msfit|LINK User opens mysite.com/products.aspx?product_id=1234 In my Page_Load() I decide whether the product with such id exists or not. If not I want user to see my custom error 404 (which is how to in my "/404.aspx" page). In web.config I wrote:
resources Windows Server 2012 resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Samples Retired content We’re sorry. The content you requested has been removed. You’ll be auto redirected in 1 second. MSDN Library MSDN Library MSDN Library MSDN Library Design Tools Development Tools and Languages Mobile and Embedded Development .NET Development Office development Online Services Open Specifications patterns & practices Servers and Enterprise Development Speech Technologies Web Development Windows Desktop App Development TOC Collapse the table of content Expand the table of content This documentation is archived and is not being maintained. This documentation is archived and is not being maintained. Complete Example for Error Handlers Other Versions Visual Studio 2010 .NET Framework 4 Visual Studio 2008 This code example includes elements for both page-level and application-level exception handling. Code Example Files The example consists of the following files: Web.config Global.asax Default.aspx ExceptionUtility (to be put in the App_Code folder) GenericErrorPage.aspx HttpErrorPage.aspx Http404ErrorPage.aspx DefaultRedirectErrorPage.aspx Web.config The following example shows the Web.config file. The customErrors section specifies how to handle errors that occur with file types that are mapped to ASP.NET, such as .aspx, .asmx, and .ashx files. (In IIS 6.0 and in IIS 7.0 in classic mode, static content files such as .html and .jpg files are not mapped to ASP.NET.) The settings in the example customErrors section cause any unhandled HTTP 404 (file not found) errors to be directed to the Http404ErrorPage.aspx file. These HTTP 404 erro