0

I have a simple controller in MVC 4, like this. Sorry burt i am junior in .net, maybe this is simple fix?

public class HomeController : BaseController
    {

        public ActionResult Index()
        {
            ViewBag.h1 = Resources.Language.HomeH1;
            ViewBag.Title = Resources.Language.HomeTitle;
            ViewBag.Description = Resources.Language.HomeDescription;
            return View();
        }

        public ActionResult Dataroom()
        {
            ViewBag.h1 = Resources.Language.DataRoomH1;
            ViewBag.Title = Resources.Language.DataroomTitle;
            ViewBag.Description = Resources.Language.DataroomDescription;
            return View();
        }

        public ActionResult Consultancy()
        {

            ViewBag.h1 = Resources.Language.ConsultancyH1;
            ViewBag.Title = Resources.Language.ConsultancyTitle;
            ViewBag.Description = Resources.Language.ConsultancyDescription;
            return View();
        }


        public ActionResult Services()
        {
            ViewBag.h1 = Resources.Language.ServicesH1;
            ViewBag.Title = Resources.Language.ServicesTitle;
            ViewBag.Description = Resources.Language.ServicesDescription;
            return View();
        }

        public ActionResult Contact()
        {

            ViewBag.h1 = Resources.Language.ContactH1;
            ViewBag.Title = Resources.Language.ContactTitle;
            ViewBag.Description = Resources.Language.ContactDescription;
            return View();
        }

It is working oK, when link are ok, but if someoen enters like /simpleexample etc. I want user always to redirect to home page, is this a simple fix?

Schneider
  • 2,446
  • 6
  • 28
  • 38

3 Answers3

1

Use the custom error pages for http status code 404 and always redirect to your controller action. ASP.NET MVC 404 Error Handling

Community
  • 1
  • 1
Roman Mahrer
  • 186
  • 1
  • 4
1

Add a custom Error page url in the web.config

<customErrors mode="On" defaultRedirect="~/Home/Index" />
Kalyan
  • 1,395
  • 2
  • 13
  • 26
  • Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately. Parser Error Message: Unrecognized configuration section customErrors. – Schneider Apr 03 '14 at 10:39
  • Have you put it inside `` ? – Kalyan Apr 03 '14 at 10:41
  • Yes ok but when i try to add something more to url, etc /Services6565 i still got this Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. – Schneider Apr 03 '14 at 10:46
0

Add this to config:

<customErrors mode="On">
   <error statusCode="404" redirect="~/Home/Index"/>
</customErrors> 

Or other way: Handle error with attrubute. Create attribute:

public class HandleMyError : HandleErrorAttribute
    {
        public override void OnException( ExceptionContext filterContext )
        {
            if ( filterContext.ExceptionHandled ) { return ; }
            else
            {
                string actionName = filterContext.RouteData.Values["action"].ToString();
                Type controllerType = filterContext.Controller.GetType();
                var method = controllerType.GetMethod( actionName );
                var returnType = method.ReturnType;

                if ( returnType.Equals( typeof( ActionResult ) ) || ( returnType ).IsSubclassOf( typeof( ActionResult ) ) )
                {
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "area", "" }, { "controller", "Home" }, { "action", "Index" } });
                }
            }

            filterContext.ExceptionHandled = true;
        }
    }

And add [HandleMyError] attribute your Home controller:

[HandleMyError]
public class HomeController : BaseController
{
}
Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90