2
I need to use the TempData
but 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 on some forums that I need to enable something but I didn’t understand straight 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 the cshtml
to see if the TempData
is null and as is a redirect to the same page use it.
You are using Owin?
– Jéf Bueno
No man, to be honest I never used
– Marlyson
I always used Tempdata in ASP.NET but now in 5 is with this novelty
– Marlyson
Okay, hold on. You’re using ASP.NET MVC 5 or ASP.NET CORE (which was formerly called ASP.NET 5, or vNext)?
– Jéf Bueno
Now in this project I am using ASP.NET 5 Web Aplication, so much so that I can still use the tags of 5 in . cshtml
– Marlyson
Could you add your code? How are you doing to use
TempData
?– Randrade
So it’s ASP.NET CORE, not MVC 5
– Jéf Bueno
I’m a little lost in these versions of ASP.NET, every time I need to install the visual studio is a different standard, I was using ASP.NET 5 so much is that the tags are ASP.NET 5 standard per example Asp-controller=""
– Marlyson
I was able to resolve by following this link http://www.mikesdotnetting.com/article/270/sessions-in-asp-net-core-1-0
– Marlyson
I had to add two lines in project.json plus a few lines in Startup.Cs, but I didn’t understand why of this
– Marlyson