简体   繁体   中英

ASP.NET Core MVC redirect with new url parameter

// Home/Index/asd-qwe 
public async Task<IActionResult> Index(string id)
{
    // id=asd-qwe
    var response = await _service.GetPost(id);

    // I want to redirect /Home/Index/response.RedirectObject.Path
    //  /Home/Index/new-asd-qwe-123 with object (response.Item)
    if (response.RedirectObject != null)
    {
        return View(response.Item); // with new url parameter /Home/Index/new-asd-qwe-123
    }

    // else return view with object         
    return View(response.Item);
}

/Home/Index/asd-qwe -> /Home/Index/new-asd-qwe-123 # with response Item

Is it possible to do this, without calling Index action again?

You can try the following. But be careful of redirection looping.

// Home/Index/asd-qwe 
public async Task<IActionResult> Index(string id)
{
    // id=asd-qwe
    var response = await _service.GetPost(id);

    // I want to redirect /Home/Index/response.RedirectObject.Path
    //  /Home/Index/new-asd-qwe-123 with object (response.Item)
    if (response.RedirectObject != null)
    {
        return RedirectToAction(nameof(Index), new { id = response.RedirectObj.Path }); // with new url parameter /Home/Index/new-asd-qwe-123
    }

    // else return view with object         
    return View(response.Item);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM