TempData[]在IIS服务器上不起作用,但在本地计算机上起作用。

huangapple go评论57阅读模式
英文:

TempData[] does not work on the IIS server, but works on the local computer

问题

我有一个控制器,输出一条消息,然后重定向到Index()。问题是,这段代码在本地机器上能正常运行,但在IIS上发布时,无法输出消息,而其他代码则正常工作。控制器代码:

public ActionResult Index()
{
    return View();
}

[HttpPost]
public ActionResult LoadFile(HttpPostedFileBase fileExcel)
{
    if (ModelState.IsValid)
    {
        try
        {
            #region check
            if (fileExcel == null)
            {
                TempData["AlertMessage"] = "aboba";
                return RedirectToAction("Index");  
            }
            #endregion
            ...
        }
        ...
    }
}

IIS版本为10.0

我尝试用Session替换TempData,但没有帮助。

英文:

I have a controller that outputs a message and then redirects to Index(). The problem is that the code works properly on the local machine, and when publishing on IIS does not want to output a message, and the rest of the code works correctly. Controller Code:

public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LoadFile(HttpPostedFileBase fileExcel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    #region check
                    if (fileExcel == null)
                    {
                        TempData["AlertMessage"] = "aboba";
                        return RedirectToAction("Index");  
                    }
                    #endregion
                ...

                }
            }
        }

IIS version 10.0

I tried to replace TempData with Session, but it didn't help.

答案1

得分: 0

默认情况下,TempData 存储在会话状态中,这意味着 Web 应用程序必须启用会话。如果禁用了 TempData,它将无法正常工作。

您可以通过查找以下行来检查应用程序的 Web.config 文件中是否启用了会话状态:

<sessionState mode="InProc" />
英文:

By default, TempData is stored in session state, which means that the web application must have sessions enabled. If TempData is disabled, it will not work as expected.

You can check if session state is enabled in your application Web.config file by looking for the following line:

&lt;sessionState mode=&quot;InProc&quot; /&gt;

huangapple
  • 本文由 发表于 2023年4月11日 09:32:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定