I need to use TempData
in a web application but it gives the following error in runtime :
InvalidOperationException: Session has not been configured for this application or request.
em Microsoft.AspNet.Http.Internal.DefaultHttpContext.get_Session()
I saw in some forums that I need to enable something but I did not understand straight away what. Does anyone know how to solve this?
This is my controller Home
:
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(Contato contato)
{
SendEmail EnviarEmail = new SendEmail();
try
{
EnviarEmail.EnviarEmail(contato);
TempData["Mensagem"] = "Mensagem enviada com sucesso!";
}
catch (Exception ex)
{
ViewBag.Message = "Erro ao enviar email!";
}
return RedirectToAction("Index");
This is my index.cshtml
<form asp-controller="Home" asp-action="Index">
@if (TempData["Mensagem"] != null)
{
<script language="JavaScript" type="text/javascript">
var texto = "@TempData["Mensagem"]";
alert(texto)
</script>
}
<div class="formulario">
<label for="nome">Nome</label>
<input type="text" name="nome" id="nome" placeholder="Nome" required oninvalid="setCustomValidity('Insira o seu nome')" onchange="try{setCustomValidity('')}catch(e){}">
</div>
<div class="formulario">
<label for="email">Email</label>
<input id="email" name="email" placeholder="Email" type="email" required oninvalid="setCustomValidity('Insira um email válido')" onchange="try{setCustomValidity('')}catch(e){}">
</div>
<div class="formulario">
<label for="assunto">Assunto</label>
<input id="assunto" name="assunto" placeholder="Assunto" type="text" required oninvalid="setCustomValidity('Insira um assunto')" onchange="try{setCustomValidity('')}catch(e){}">
</div>
<div class="formulario">
<label for="mensagem">Mensagem</label>
<textarea id="mensagem" name="mensagem" placeholder="Mensagem" required oninvalid="setCustomValidity('A mensagem é obrigatória')" onchange="try{setCustomValidity('')}catch(e){}"></textarea>
</div>
<input class="botao_enviar" type="submit" value="Enviar">
</form>
My site is a single page site where I navigate between sessions, so I put this if
in cshtml
to see if TempData
is null and how is a redirect to the same page use it .