Doubt with Handleerror Asp.net mvc

Asked

Viewed 42 times

1

I am searching for a way to avoid an error message when user happens enters for 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 is recorded on his computer. In this case it would have some route to direct the user stating that it was not possible or direct him to the index?

I took a test:

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

Upshot:

inserir a descrição da imagem aqui

1 answer

0

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

One of the ways to avoid this error is by making the parameter nullable.

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

    return View();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.