我正在尝试使用自定义ITempDataProvider提供程序将TempData存储在浏览器的cookie而不是会话状态中.但是,一切正常,但我无法在读取后从Response流中删除cookie.
有任何想法吗?
谢谢!
public class CookieTempDataProvider : ITempDataProvider
{
internal const string TempDataCookieKey = "__ControllerTempData";
HttpContextBase _httpContext;
public CookieTempDataProvider(HttpContextBase httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
_httpContext = httpContext;
}
public HttpContextBase HttpContext
{
get
{
return _httpContext;
}
}
protected virtual IDictionary<string, object> LoadTempData(ControllerContext controllerContext)
{
HttpCookie cookie = _httpContext.Request.Cookies[TempDataCookieKey];
if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
{
IDictionary<string, object> deserializedTempData = DeserializeTempData(cookie.Value);
// Remove cookie
cookie.Expires = DateTime.MinValue;
cookie.Value = string.Empty; …Run Code Online (Sandbox Code Playgroud) 在ASP.NET MVC中,有一个TempData可以将数据从一个页面传递到另一个页面.在ASP.NET中,这相当于什么?
因为我一遍又一遍地使用相同的TempData键,所以我想更容易跟踪这些键.如果最终我能写出这样的东西会很棒:
MyTempData.Message = "my message";
Run Code Online (Sandbox Code Playgroud)
代替
TempData["Message"] = "my message";
Run Code Online (Sandbox Code Playgroud) 我一直在使用TempData很长时间,并且面临着一些奇怪的问题.我有基本情景:
[HttpPost]
public ActionResult Create(ProductCreateModel newProduct)
{
// create and save product to db
// try upload product to external site
try { UploadProductToEbay(newProduct); }
catch {
TempData["error"] = "error";
return RedirectToAction("Edit", newProduct.Id);
}
...
}
[HttpGet]
public ActionResult Edit(int Id)
{
var error = TempData["error"]; // at this point temp data collection is empty and have no idea why
...
}
Run Code Online (Sandbox Code Playgroud)
上传失败并return RedirectToAction("Edit", newProduct.Id);执行行时会发生此问题.丢失临时数据值的原因可能不是很明显?
更新: 当我使用时
TempData["error"] = "error";
RedirectToAction(...);
Run Code Online (Sandbox Code Playgroud)
在catch块之外一切正常,临时数据值转移到Edit动作.
我有一个类似于以下内容的控制器操作,TempData由我的框架初始化。我注意到,如操作“ EmployeeUnderAge”所示,一旦读取该值,TempData不会清除该值。
TempData何时清除已读取的数据?
public class HomeController : Controller
{
public ActionResult Index(int iD)
{
var employeeAge = (int)TempData["Age"];
RouteData.Values.Add("Age", employeeAge);
return RedirectToAction("EmployeeUnderAge");
}
public ActionResult EmployeeUnderAge(int employeeAge)
{
var stillInTempData = (employeeAge == ((int) TempData["Age"]));
return (stillInTempData) ? View("Index") : View("Error");
}
}
Run Code Online (Sandbox Code Playgroud) 我要检查TempData的内部if状态。但是我遇到了错误。
我的控制器
public ActionResult Customer(PurchaseViewModel purchaseviewmodel)
{
TempData["Fromdt"] = purchaseviewmodel.FromDate;
TempData["todt"] = purchaseviewmodel.ToDate;
If(TempData["Fromdt"] == Convert.ToDateTime(“01/01/0001”)&& TempData["todt"] == Convert.ToDateTime(“01/01/0001”))
{
//...
}
else
{
//...
}
return View(Customer);
}
Run Code Online (Sandbox Code Playgroud)
为什么在Tempdata中获取模型值意味着我想将在TempDate中获取的值传递给另一个动作。所以只有我在使用TempData。现在我出错了。错误是
运算符==不适用于对象和System.DateTime之间。
我尽力解释了这个问题。因此,任何人都可以帮助我解决此问题。而且我只需要TempData而不是将值直接存储在变量中。我可以将值存储在像
var fmdt = purchaseviewmodel.FromDate;
var todt = purchaseviewmodel. ToDate;
Run Code Online (Sandbox Code Playgroud)
但是我只需要在TempData中存储值,这是我的要求,因为我需要在另一个操作中使用该TempData值。我需要另一个目的
我正在使用伪造的 HttpContext (最后提供的代码),可能我错过了一些东西,因为我无法访问 TempData 集合(SetFakeControllerContext 方法的第四行)。每次我尝试时都会收到此错误消息:
“controller.TempData”引发了“System.AccessViolationException”类型的异常
调用FakeHttpContext的代码是:
public static void SetFakeControllerContext(this Controller controller)
{
HttpContextBase httpContext = FakeHttpContext(string.Empty);
var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
controller.ControllerContext = context;
controller.TempData = new TempDataDictionary(); //This is not necessary! It was just a test!!!!
}
Run Code Online (Sandbox Code Playgroud)
有人知道我做错了什么吗?
public static HttpContextBase FakeHttpContext(string username){
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new …Run Code Online (Sandbox Code Playgroud) \xc4\xb0n 我的 MVC3 项目,有大量的 TempData[] 用于在操作之间传递数据。当我使用 Chrome 时,它的效果非常完美。但在 IE 中我无法获取 TempData[] 项的值。如果有人知道问题是什么以及我该如何解决它?
\n\npublic class SomeController : Controller\n{\n public ActionResult SomeAction()\n {\n TempData["id"] = "someData";\n return View();\n\n }\n}\n\n\npublic class AnotherController : Controller\n{\n public ActionResult AnotherAction()\n {\n string data = Convert.ToString(TempData["id"]);\n return View();\n\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n`
\ninternet-explorer google-chrome tempdata razor asp.net-mvc-3
还行吧:
(bool)TempData["sortBool"]
Run Code Online (Sandbox Code Playgroud)
这不行:
TempData["sortBool"] as bool
Run Code Online (Sandbox Code Playgroud)
该错误指出:
Error 1 The as operator must be used with a reference type or nullable type ('bool' is a non-nullable value type) C:\Users\xye15\Desktop\CodeChallengeV4\CodeChallengeV4\MovieKiosk\Controllers\HomeController.cs 55 21 MovieKiosk
Run Code Online (Sandbox Code Playgroud)
我明白为什么第二个是错误的,就像错误消息一样。但是我很困惑为什么编译器不抱怨第一个。谢谢!
我想在我的.net核心mvc应用程序中使用TempData。我遵循了https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata上的文章
我总是得到NULL这是我的代码:
public async Task<ActionResult> Index(RentalsFilter filter)
{
TempData["test"] = "ABC";
return View();
}
public ActionResult Create()
{
var abc = TempData["test"].ToString();
return View();
}
Run Code Online (Sandbox Code Playgroud) tempdata ×10
asp.net-mvc ×5
c# ×4
asp.net ×2
.net ×1
cookies ×1
httpcontext ×1
mocking ×1
moq ×1
razor ×1