0

Am trying to use ELMAH with MVC4 and configured using NUGET Package for MVC.

It handles all the exceptions happening in my code and am logging it to SQL Server.

Using the Ajax Error function am handling AJAX Exceptions(500) Error too. Everything works fine and redirected Error page overriding OnException in Filter Config

But if the user changes the URL and and if the controller doesnt exist am getting Error and not redirected to the Error page.

Ex:

sitename/test -- Correct URL

sitename/tst ---- Getting Server Error instead redirecting to Error page

What is the correct way to handle 404 using ELMAH and redirecting to friendly 404 page in MVC.

I tried changing the Custom Error with 404 in web.config but it didn't worked as expected.

By default all Errors other than 404 am re directing to Error page

Am trying to redirect to friendly 404 page rather than server error page.

Any useful links or code will be helpful .Thanks

============================================

Update web.config

<customErrors mode="On">
<error statusCode="500" redirect="/Views/Shared/Error.cshtml"/>
<error statusCode="404" redirect="/Views/Shared/ErrorNotFound.cshtml"/>
</customErrors>
Peru
  • 2,871
  • 5
  • 37
  • 66
  • `I tried changing the Custom Error with 404 in web.config but it didn't worked as expected` Post your web.config. What did it do? What did you expect it to do? – cadrell0 Aug 05 '13 at 16:37
  • try this first. http://stackoverflow.com/questions/16700379/error-pages-not-found-throwing-elmah-error-with-custom-error-pages – zs2020 Aug 05 '13 at 16:41

1 Answers1

2

In ASP.NET MVC, your URL does not represent the View you are displaying. It is the Controller Action. You cannot navigate to a View.

For example, the url ~/Home/Index, does not point to Views/Home/Index.cshtml. It points to the Index method of Controllers/HomeController. This method then renders the Index.cshtml view and returns the result.

So first, you need to create an ErrorController. Then, you need to move your Error Views into a matching Error folder. Now add action methods for Error and ErrorNotFound to your ErrorController, and have them return the correct View.

Finally, change your web.config redirect URL's to ~/Error/ErrorNotFound.

Using this method, you will probably want to change the names of the Controller Actions, and their respective Views to remove the duplicated work Error.

cadrell0
  • 17,109
  • 5
  • 51
  • 69