使用TempData的崩溃我的应用程序 [英] Using Tempdata is crashing my application

查看:226
本文介绍了使用TempData的崩溃我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的ASP.NET和我试图用一个ASP.Net 5 preVIEW模板传递两个控制器之间的一个对象,我在Visual Studio 2015的Web应用程序使Web应用程序Web应用程序(如果有帮助,我想我使用测试版code 7,我的不可以建设DNX核心5)。

I'm very new to ASP.NET and am attempting to pass an object between two controllers in a web application I'm making in Visual Studio 2015. The web application is using an ASP.Net 5 Preview Template Web application (if it helps, I think I'm using beta code 7 and I'm not building for DNX Core 5).

我遇到的问题是每当我试图把的任何的到TempData的变量,程序似乎崩溃。例如,在创建的方法我有:

The problem I'm having is whenever I try to put anything into the TempData variable, the program seems to crash. For example, in a "Create" method I have:

        [HttpPost]
    public ActionResult Create(Query query)
    {
        switch (query.QueryTypeID)
        {
            case 1:
                TempData["Test"] = "Test";
                return RedirectToAction("Index", "EventResults");
            case 2:
                break;
            default:
                break;
        }
        return View();
    }

在该方法中,我试图在关键测试添加一个简单的测试字符串。当我运行在那里,TempData的语句的应用程序,我收到一条错误信息,说明

In that method, I attempt to add a simple test string under the key "test". When I run the application with that TempData statement in there, I receive an error message stating

在处理请求时发生未处理的异常。

An unhandled exception occurred while processing the request.

出现InvalidOperationException:会话没有配置这个应用程序>或请求。
  Microsoft.AspNet.Http.Internal.DefaultHttpContext.get_Session()

InvalidOperationException: Session has not been configured for this application >or request. Microsoft.AspNet.Http.Internal.DefaultHttpContext.get_Session()

我试图要位于该项目的wwwroot的元件和增加一个的sessionState对象到一个的System.Web元素的Web.config,但是这对错误没有影响。

I have tried going to the Web.config located in the wwwroot element of the project and adding a "sessionState" object into a "system.web" element, but this had no effect on the error.

所以AP preciated,因为我一直在寻找这个解决方案无处不在任何帮助将是非常多。我希望这是愚蠢的东西/一清二楚的是,我莫名其妙地错过了。

Any help would be very much so appreciated as I've been looking for solutions for this everywhere. I'm hoping it's something stupid/blindingly obvious that I somehow missed.

推荐答案

为了使用中间件,如会议,高速缓存等在ASP.NET 5,你必须明确地启用它们。

In order to use middleware, such as Session, Cache, etc in ASP.NET 5, you have to enable them explicitly.

启用会话是由你的 project.json 文件的依赖关系部分添加相应的NuGet包进行(请确保包版本,您有其他依赖的版本相匹配添加):

Enabling session is done by adding the appropriate nuget package in your project.json file's dependencies section (make sure that the package version matches the versions of the other dependencies you have added):

"Microsoft.AspNet.Session": "1.0.0-*"

和相应的会话(缓存)存储包,以及(如下面的例子;在内存中):

and the appropriate session (cache) storage package as well (like the example below; in memory):

"Microsoft.Extensions.Caching.Memory": "1.0.0-*"

和添加中​​间件进行依赖于 Startup.cs 服务配置分辨率:

and adding the middleware to dependency resolution in the Startup.cs Service configuration:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCaching();
    services.AddSession(/* options go here */);
}

,并在 Startup.cs OWIN配置添加中间件OWIN:

and adding the middleware to OWIN in the Startup.cs OWIN configuration:

public void Configure(IApplicationBuilder app)
{
    app.UseSession();
    //...

请确保 UseSession 自带的的MVC的配置。

Make sure that the UseSession comes before the MVC configuration.

这篇关于使用TempData的崩溃我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆