Error when using TempData in ASP.NET 5: Session has not been configured for this application or request

2

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 .

    
asked by anonymous 31.07.2016 / 17:50

1 answer

1

I believe the problem is another. Take a look if you have the referenced Microsoft.AspNet.Session package.

After this, check out the order in Startup.cs:

// Add MVC services to the services container.
services.AddMvc();
services.AddCaching(); // Adds a default in-memory implementation of IDistributedCache
services.AddSession();

and also

// IMPORTANT: This session call MUST go before UseMvc()
app.UseSession();

// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller}/{action}/{id?}",
        defaults: new { controller = "Home", action = "Index" });

    // Uncomment the following line to add a route for porting Web API 2 controllers.
    // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
});

source: link

    
31.07.2016 / 22:51