0

Sorry, if question is not clear, but I need "file not found" custom page, not "Page not found". If you type at the end of url .html - it do not will go thru Routes.

I have route which catch all routes:

routes.MapRoute(
            name: "Default",
            url: "{*url}",
            defaults: new { controller = "Home", action = "Index", url = UrlParameter.Optional },
            constraints: new { Domain = new DomainsConstraint() }
        );

But how can I make custom page for file not found? If at the and of url I add for example .html - It will show me 404 file not found

Andrey
  • 311
  • 2
  • 8
  • 29
  • What exactly are you looking for ? do you want to redirect to html page from your controller's action method ? or you want to check if such action method or controller is exist or not ? – Mox Shah Mar 31 '15 at 05:41
  • If i have url like myUrl.com/home.html - it will show me file not found. I need just replace this page to custom – Andrey Mar 31 '15 at 05:47

3 Answers3

1

Simply set error configuration in web.config file

Web.config

 <customErrors mode="On" defaultRedirect="~/Error">
          <error redirect="~/Controller/Error" statusCode="404" />
    </customErrors>

Controller

public ActionResult Error(){
   var result = new FilePathResult("~/404.html", "text/html");
   return result;
}
Mox Shah
  • 2,967
  • 2
  • 26
  • 42
0
routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "StaticContent", action = "PageNotFound" }
    );  

You can create your own custom error page :

<customErrors mode="On" defaultRedirect="~/Error">
      <error redirect="~/StaticContent/PageNotFound" statusCode="404" />
</customErrors>

public class StaticContent : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult PageNotFound()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("PageNotFound");
    }
}
Ram Khumana
  • 844
  • 6
  • 14
0

You can manage it from Global.asax file. You can get perfect solution from this answer from here

Community
  • 1
  • 1
Ruchir Shah
  • 316
  • 2
  • 8