1

I am a newbie to MVC.

I am trying to create a custom error page to show when the user is looking for an unavailable resource. I followed a few articles and was able to show a custom error page. ASP.NET MVC 404 Error Handling.

I have created an error Controller with 'NotFound404()' action method and view is created with the same name 'NotFound404.cshtml'.

public class ErrorController : Controller  
{
    // GET: Error  
    public ActionResult NotFound404()  
    {  
        return View();  
    }  
}  

Web.config

<system.web>  
  <customErrors mode="On">  
    <error statusCode="404" redirect="~/Error/NotFound404"/>  
  </customErrors>  
</system.web>

When I try to access any unknown resource, I can see the notfound404. But when I inspect URL it shows as 'http://localhost:xyzw/Error/NotFound404?aspxerrorpath=/New'.

Why do i get such weird URL? and I am not using any aspx pages but it shows aspx in the URL. HTTP status under F12-Developer tools- Network - shows 200 which isn't correct. How do I rectify this?

And when I tried to access any static HTML page instead of a new controller/ action-view

<system.web>  
  <customErrors mode="On">  
    <error statusCode="404" redirect="~/Shared/NotFound.html"/>  
  </customErrors>  
</system.web>

404 Error occurs -- is this error because it is looking up for wrong path?

How do I fix these errors?
Please show a right way to configure these error handling strategies.

Thanks in advance.

mstill3
  • 132
  • 1
  • 2
  • 13
Learn Avid
  • 59
  • 2
  • 13
  • Try `redirect="/Shared/NotFound.html"`. – Anis Alibegić Mar 26 '18 at 19:29
  • I don't think you need to have the `~` in your URLs. This might be slightly different to your exact issue but the following config will allow you to configure custom errors without the URL changing (using `httpErrors` instead of `CustomErrors`) https://stackoverflow.com/a/32117966/4457454 – scgough Mar 26 '18 at 21:04
  • Thanks @Spectarion & @scgough for your response. I tried `redirect="/Shared/NotFound.html"` and when i try accessing any unknown resource, it still shows HTTP404 error screen. – Learn Avid Mar 27 '18 at 15:51

0 Answers0