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

Asked

Viewed 858 times

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?

  • No man, to be honest I never used

  • I always used Tempdata in ASP.NET but now in 5 is with this novelty

  • Okay, hold on. You’re using ASP.NET MVC 5 or ASP.NET CORE (which was formerly called ASP.NET 5, or vNext)?

  • 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

  • Could you add your code? How are you doing to use TempData?

  • So it’s ASP.NET CORE, not MVC 5

  • 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=""

  • I was able to resolve by following this link http://www.mikesdotnetting.com/article/270/sessions-in-asp-net-core-1-0

  • I had to add two lines in project.json plus a few lines in Startup.Cs, but I didn’t understand why of this

Show 5 more comments

1 answer

1

I think the problem is something else. Take a look at whether Voce has the Microsoft.AspNet.Session package referenced.

After that, check the order 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: http://benjii.me/2015/07/using-sessions-and-httpcontext-in-aspnet5-and-mvc6/

Browser other questions tagged

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