Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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# 如何使用CreatedAtRoute的内容对象?_C#_Asp.net Core - Fatal编程技术网

C# 如何使用CreatedAtRoute的内容对象?

C# 如何使用CreatedAtRoute的内容对象?,c#,asp.net-core,C#,Asp.net Core,如何使用通过CreatedAtRoute发送的对象而不重新创建 例如: return CreatedAtRoute(nameof(GetUserById), new { Id = userReadDto.Id }, userReadDto); 我想使用userReadDto对象,而不在中重新创建: [HttpGet("{id}", Name = "GetUserById")] public ActionResult<

如何使用通过CreatedAtRoute发送的对象而不重新创建

例如:

return CreatedAtRoute(nameof(GetUserById), new { Id = userReadDto.Id }, userReadDto);
我想使用userReadDto对象,而不在中重新创建:

        [HttpGet("{id}", Name = "GetUserById")]
        public ActionResult<UserReadDto> GetUserById(int id)
        {
            var user = _repository.GetUserById(id);
            if (user != null)
                return Ok(_mapper.Map<UserReadDto>(user));
            else
                return NotFound();
        }
如何使用CreatedAtRoute的内容对象

您可以检查有关以下内容的定义:

我们可以看到,第一个参数是路由名称,而不是操作名称。您可以检查CreatedAtRoute方法的以下代码:

在Startup.Configure方法中,创建单个路由并命名为default:

        app.UseEndpoints(endpoints =>
        {  
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index2}/{id?}");
            endpoints.MapRazorPages();
            
        });
然后,在控制器中,我们可以使用如下CreatedAtRoute方法:

 return CreatedAtRoute("default", new { Id = 101 }, result); //the first parameter is the route name.
根据您的代码,我假设您希望导航到GetUserById操作方法,并在操作方法之间传输数据userReadDto。如果是这种情况,您可以使用方法,代码如下:

TempData.Put("data",result); //using TempData or Session to store the data.
return RedirectToAction(nameof(GetUserById), new { Id = userReadDto.Id });
然后,在GetUserById方法中

    [HttpGet("{id}", Name = "GetUserById")]
    public ActionResult<UserReadDto> GetUserById(int id)
    {
        var data = TempData.Get<List<UserReadDto>>("data");

        var user = _repository.GetUserById(id);
        if (user != null)
            return Ok(_mapper.Map<UserReadDto>(user));
        else
            return NotFound();
    }
[注意]上述使用TempDate存储对象数据的代码,必须使用以下代码添加TempDataHelper:

//required using Microsoft.AspNetCore.Mvc.ViewFeatures;
//required using System.Text.Json;
public static class TempDataHelper
{
    public static void Put<T>(this ITempDataDictionary tempData, string key, T value) where T : class
    {
        tempData[key] = JsonSerializer.Serialize(value);
    }

    public static T Get<T>(this ITempDataDictionary tempData, string key) where T : class
    {
        tempData.TryGetValue(key, out object o);
        return o == null ? null : JsonSerializer.Deserialize<T>((string)o);
    }

    public static T Peek<T>(this ITempDataDictionary tempData, string key) where T : class
    {
        object o = tempData.Peek(key);
        return o == null ? null : JsonSerializer.Deserialize<T>((string)o);
    }
}

有关使用TempData和Session的更多详细信息,请选中

CreatedataRoute用于指定路由,您是否希望改为创建路由?还有,你所说的不重新创造是什么意思?不重新创建什么?我的意思是我已经发送了一个对象,如何在GetUserById中使用它而不重新驱动?对不起,语法规则。