If you type a random path into the URL, the website will return the standard ASP.NET 404 html response if custom errors is set to "RemoteOnly". When custom errors is set to "On" it will return a custom 404 view that I have made.
However, if I use the following line of code:
return new HttpNotFoundResult();
The browser connection appears to just terminate with no response being returned. Not the behavior I wanted or expected. How can I get the expected behavior without actually instantiating and executing the page not found view? I have a feeling that the behavior
I want only works that way when HttpExceptions occur. Perhaps I just need to throw an HttpException with 404 status code instead of using the line of code above?
In my opinion, the HttpNotFoundResult could be used to the scenario of the necessary data isn’t exist. For this result,
redirect to the custom page, you need to specify the error page in the server (system.webServer section).
For the web.config, the code will be like this:
<httpErrors errorMode="Custom" defaultResponseMode="Redirect"> <remove statusCode="401" subStatusCode="-1" /> <remove statusCode="500" /> <error statusCode="401" prefixLanguageFilePath="" path="ErrorTest" responseMode="ExecuteURL" /> <error statusCode="404" subStatusCode="0" path="ErrorTest" responseMode="Redirect" /> <error statusCode="400" path="ErrorTest" responseMode="Redirect" /> <error statusCode="500" path="ErrorTest" responseMode="Redirect" /> </httpErrors>
If you want to have your own logic to deal with it, you need to throw an exception.
Try using HttpStatusCodeResult instead of HttpNotFoundResult, through which u can send 404 to browser.
When I use HttpNotFoundResult, browser displays the standard 404 error message.
I just tried out one more time and it is showing the 404 error.
HttpNotFoundResult sends 404 to the browser and behaves the same way as HttpNotFoundResult. Not sure you understand the question?
I’m pretty convinced at this point, that throwing an HttpException with HttpStatusCode.NotFound is the only way to get the behavior I want.
Now I understood your question. It seems that HttpNotFoundResult is intended to send the response directly to the browser without processing anyother server side resources (like custom errors). As mentioned by you, I guess you need to use HttpException or
Exactly. But HttpStatusCodeResult seems to just send directly to the browser as well.
See this blog,
As I mentioned in my previous reply, you could specify the custom error page in the server level. (system.webServer section)