以下是我的问题:
为什么会发生这种情况,我该如何纠正?
<HttpPost()> _
<HttpParamAction()> _
Function Upload(ByVal model As MaxDocument, formcollection As FormCollection) As ActionResult
Dim sCriteria As String = ""
Dim nKeyIndex As Integer = 0
Dim nFieldIndex As Integer = -1
Dim sFieldValue As String = ""
Try
' Build sCriteria from the submitted formcollection
model.GetFileCabinetFieldList()
For nFieldIndex = 0 To (model.IndexFieldCount - 1)
sFieldValue = ""
If nFieldIndex > 0 Then
sCriteria += "~"
End If
Dim fcf As MaxServerLib.FileCabinetField = model.criterionAtIndex(nFieldIndex)
' Get the field value corresponding to this field
For Each oKey As Object In formcollection.AllKeys
If oKey.ToString = fcf.sFieldName Then
sFieldValue = formcollection(oKey.ToString)
Exit For
End If
Next
sCriteria += sFieldValue
Next
If sCriteria = "" Then sCriteria = "[BlankIndex]"
' Set the criteria property of the model, which will be used for both field validation and document export.
model.Criteria = sCriteria
' First thing we do is to perform valiation of the criteria
model.ValidateFieldValues()
If Not model.AllFieldValuesValid() Then
' Handle case where one or more field values are invalid.
' In this case we want to redisplay the form but show an error message listing the invalid fields
model.HasAttemptedUpload = True
' Set tempData before redirect:
TempData("MaxDocument") = model
Return RedirectToAction("Index")
Else
' All field values are valid, now attempt to add the document
...
End If
Catch ex As Exception
System.Diagnostics.Debugger.Break()
End Try
'End If
' If we got this far, something failed, redisplay form
Return View(model)
End Function
编辑:
似乎正在发生的事情是浏览器缓存了来自第一篇文章的帖子操作,并且在每个后续帖子(第一篇文章之后)它呈现第一篇文章的缓存结果而不是呈现当前帖子的结果。为什么会这样做?
答案 0 :(得分:0)
我创建了一个班级NoCacheControlAttribute
<强> NoCacheControlAttribute.cs:强>
using System;
using System.Web;
using System.Web.Mvc;
namespace Company.Common.Web.MVC
{
public class NoCacheControlAttribute : ActionFilterAttribute
{
private readonly HttpCacheability _cacheability;
public NoCacheControlAttribute(HttpCacheability cacheability)
{
_cacheability = cacheability;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
cache.SetAllowResponseInBrowserHistory(false);
cache.SetCacheability(_cacheability);
cache.SetExpires(DateTime.Now);
cache.SetNoServerCaching();
cache.SetNoStore();
}
}
}
我通过Autofac Dependency Injection / Constructor Injection获得了cacheability参数。
<强> ... Controller.cs 强>
using ...
using System.Web;
using System.Web.Mvc;
using Company.Common.Web.MVC;
using ...
namespace Company.Project.Web.Controllers
{
[NoCacheControl(HttpCacheability.NoCache)]
public class ContractController : Controller
{
private readonly IRepository _repository;
public ContractController(IRepository repository)
{
_repository = repository;
}
[HttpGet]
public ActionResult ActionTest(string id)
{
...
return ...;
}
}
}
所以,禁用缓存!