Reactjs 如何在ASP.NET核心应用程序中使用TempData
我在React中创建了一个ASP.NET Core 3.1 web应用程序 我试图在控制器操作方法中使用Reactjs 如何在ASP.NET核心应用程序中使用TempData,reactjs,asp.net-core,Reactjs,Asp.net Core,我在React中创建了一个ASP.NET Core 3.1 web应用程序 我试图在控制器操作方法中使用TempData。但是我有一个错误,说 当前上下文中不存在TempData 如何使用它?Controller继承自ControllerBase,因此Controller拥有ControllerBase所拥有的一切。我认为将ControllerBase更改为Controller是更好的方法 您可以检查以下源代码: public abstract class Controller : Contro
TempData
。但是我有一个错误,说
当前上下文中不存在TempData
如何使用它?
Controller
继承自ControllerBase
,因此Controller
拥有ControllerBase
所拥有的一切。我认为将ControllerBase
更改为Controller
是更好的方法
您可以检查以下源代码:
public abstract class Controller : ControllerBase, IActionFilter, IFilterMetadata, IAsyncActionFilter, IDisposable
{
protected Controller();
public dynamic ViewBag { get; }
[ViewDataDictionary]
public ViewDataDictionary ViewData { get; set; }
public ITempDataDictionary TempData { get; set; }
public void Dispose();
//....
}
如果必须使用ControllerBase
。下面是一个关于如何在api控制器中使用TempData
的工作演示:
[ApiController]
[Route("[controller]")]
public class ValuesController : ControllerBase
{
private readonly ITempDataDictionaryFactory _tempDataDictionaryFactory;
public ValuesController(ITempDataDictionaryFactory tempDataDictionaryFactory)
{
_tempDataDictionaryFactory = tempDataDictionaryFactory;
}
[HttpGet]
public void Get()
{
var tempData = _tempDataDictionaryFactory.GetTempData(HttpContext);
tempData["EmailId"] = "adasd";
//do your stuff...
}
}
更新:
仍然重复我在Get
方法中所做的操作,并在其他操作中获取tempData,如下所示:
var tempData = _tempDataDictionaryFactory.GetTempData(HttpContext);
var data = tempData["EmailId"];
Controller
继承自ControllerBase
,因此Controller
拥有ControllerBase
所拥有的一切。我认为将ControllerBase
更改为Controller
是更好的方法
您可以检查以下源代码:
public abstract class Controller : ControllerBase, IActionFilter, IFilterMetadata, IAsyncActionFilter, IDisposable
{
protected Controller();
public dynamic ViewBag { get; }
[ViewDataDictionary]
public ViewDataDictionary ViewData { get; set; }
public ITempDataDictionary TempData { get; set; }
public void Dispose();
//....
}
如果必须使用ControllerBase
。下面是一个关于如何在api控制器中使用TempData
的工作演示:
[ApiController]
[Route("[controller]")]
public class ValuesController : ControllerBase
{
private readonly ITempDataDictionaryFactory _tempDataDictionaryFactory;
public ValuesController(ITempDataDictionaryFactory tempDataDictionaryFactory)
{
_tempDataDictionaryFactory = tempDataDictionaryFactory;
}
[HttpGet]
public void Get()
{
var tempData = _tempDataDictionaryFactory.GetTempData(HttpContext);
tempData["EmailId"] = "adasd";
//do your stuff...
}
}
更新:
仍然重复我在Get
方法中所做的操作,并在其他操作中获取tempData,如下所示:
var tempData = _tempDataDictionaryFactory.GetTempData(HttpContext);
var data = tempData["EmailId"];
请在此添加一些代码,我们无法获取您的问题properly@BipilRaut,我想要类似于
TempData[“EmailId”]=viewModel.EmailAddress的内容代码>但我得到一个错误:当前上下文中不存在TempData'。您好@Dyantest,我认为您需要检查控制器是否继承了controller
。如果您继承了ControllerBase
,它将给出这样的错误。您好@Rena,是的。你是对的。它继承了ControllerBase
。因为它是一个api控制器。你能帮助我如何在Api控制器中使用TempData
(我的项目在ReactJS
)吗。嗨@Dyantest,controller
继承自ControllerBase
,所以Controller
拥有ControllerBase
所拥有的一切。我认为将ControllerBase更改为Controller是一种更好的方法。请检查我下面的答案。请在此处添加一些代码,我们无法获得您的问题properly@BipilRaut,我想要类似于TempData[“EmailId”]=viewModel.EmailAddress的内容代码>但我得到一个错误:当前上下文中不存在TempData'。您好@Dyantest,我认为您需要检查控制器是否继承了controller
。如果您继承了ControllerBase
,它将给出这样的错误。您好@Rena,是的。你是对的。它继承了ControllerBase
。因为它是一个api控制器。你能帮助我如何在Api控制器中使用TempData
(我的项目在ReactJS
)吗。嗨@Dyantest,controller
继承自ControllerBase
,所以Controller
拥有ControllerBase
所拥有的一切。我认为将ControllerBase更改为Controller是一个更好的方法。请检查我下面的答案。谢谢。但是,您能否建议如何在另一个操作方法中访问tempData[“EmailId”]
。API不需要tempData
。没有视图接收存储在其中的值,谢谢。但是,您能否建议如何在另一个操作方法中访问tempData[“EmailId”]
。API不需要tempData
。没有视图可以接收存储在那里的值