Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET MVC核心中使用POST-REDIRECT-GET_C#_Visual Studio 2015_Asp.net Core_Custom Action Filter - Fatal编程技术网

C# 在ASP.NET MVC核心中使用POST-REDIRECT-GET

C# 在ASP.NET MVC核心中使用POST-REDIRECT-GET,c#,visual-studio-2015,asp.net-core,custom-action-filter,C#,Visual Studio 2015,Asp.net Core,Custom Action Filter,在ASP.NET MVC核心项目中,我如何使用的替代方案。当我在项目的控制器文件夹中复制以下内容时,它无法识别以下对象TempData[Key],ViewData,因为它使用的是ASp.NET Mvc核心中未使用的System.Web.Mvc命名空间。我想在我的ASP.NET MVC核心项目中实现POST-REDIRECT-GET,但作者似乎没有使用MVC核心: using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filt

在ASP.NET MVC核心项目中,我如何使用的替代方案。当我在项目的控制器文件夹中复制以下内容时,它无法识别以下对象
TempData[Key],ViewData
,因为它使用的是ASp.NET Mvc核心中未使用的System.Web.Mvc命名空间。我想在我的ASP.NET MVC核心项目中实现POST-REDIRECT-GET,但作者似乎没有使用MVC核心:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace myASPCoreProject.Controllers
{

    public abstract class ModelStateTransfer : ActionFilterAttribute
    {
        protected static readonly string Key = typeof(ModelStateTransfer).FullName;
    }

    public class ExportModelStateAttribute : ModelStateTransfer
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //Only export when ModelState is not valid
            if (!filterContext.ModelState.IsValid)
            {
                //Export if we are redirecting
                if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult))
                {
                    filterContext.Controller.TempData[Key] = filterContext.ModelState;
                }
            }

            base.OnActionExecuted(filterContext);
        }
    }

    public class ImportModelStateAttribute : ModelStateTransfer
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary;

            if (modelState != null)
            {
                //Only Import if we are viewing
                if (filterContext.Result is ViewResult)
                {
                    filterContext.Controller.ViewData.ModelState.Merge(modelState);
                }
                else
                {
                    //Otherwise remove it.
                    filterContext.Controller.TempData.Remove(Key);
                }
            }

            base.OnActionExecuted(filterContext);
        }
    }
}

我们可以使用重定向到操作,正如您在Microsoft文档中看到的那样

配置DI:

   public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    // Add the temp data provider
    services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
}


[HttpPost]
public IActionResult Edit(User user)
{
    _userRepository.Save(user);
    TempData["User"] = JsonConvert.SerializeObject(user);
    return RedirectToAction("Edit", new { id = user.Id });
}
这说明了如何从DI服务获取它。
[HttpPost]
public IActionResult Edit(User user)
{

      _userRepository.Save(user);
      return RedirectToAction(nameof(UserController.Index), "User");
    }