Doubt with HandleError asp.net mvc

1

I'm looking for a way to avoid an error message when the user happens to enter a page that needs a parameter:

namespace Projeto.WEB.Controllers
{
    public class ModalidadeController : Controller
    {
        // GET: Modalidade
        public ActionResult Index()
        {
            return View();

        }

        [HandleError(ExceptionType = typeof(OverflowException), View = "Home" )]
        [HandleError()]
        public ActionResult SelecionarModalidade(int id)
        {
            return View();
        }
    }
}

The problem, if the user tries to enter directly on this page after it has been saved to your computer. In this case would it have any route to direct the user informing that it was not possible or to direct it to index ?

I tested:

<customErrors mode="On" defaultRedirect="Error" />

Result:

    
asked by anonymous 29.07.2016 / 22:39

1 answer

0

This error occurs when the expected parameter is a primitive type, that is, it is not nullable.

One way to avoid this error is to make the parameter nullable.

public ActionResult SelecionarModalidade(int? id)
{
    if(!id.HasValue)
        return new RedirectResult("~/");

    return View();
}
    
24.10.2016 / 09:50