简体   繁体   English

如何重定向到 IActionResult 的 post 重载

[英]How to redirect to the post overload of an IActionResult

I'm working on an Apsp.Net 6 project targeted .Net6 .我正在开发针对.Net6Apsp.Net 6项目。

I have this ActionResult:我有这个 ActionResult:

    [Route( nameof(Recreate))]
    public async Task<IActionResult> Recreate()
    {
       //Some code here
    }

    [HttpPost]
    [Route( nameof(Recreate))]
    public async Task<IActionResult> Recreate(StudentYearlyResultReCreateVm model)
    {
       //Some code here
    }

Now I have another ActionResult described like this:现在我有另一个ActionResult描述如下:

    [Route( nameof(RecreateWithId))]
    public async Task<IActionResult> RecreateWithId(int id)
    {
        var result = await _studentYearlyResultRepository.GetByIdAsync( id );
        var modelObj = new StudentYearlyResultReCreateVm() {StudentId = result.StudentId , AcademicYearId = result.AcademicYearId};

        return RedirectToAction( nameof( Recreate ) , modelObj );
    }

The problem: The problem is in RecreateWithId method I try to RedirectToAction to the post overload for Recreate action, but all I got is just redirected to the Get one.问题:问题出在RecreateWithId方法中,我尝试将RedirectToActionRecreate操作的后重载,但我得到的只是重定向到 Get 一个。

So please how I can redirect to the post overload of Recreate ?那么请我如何重定向到Recreate的帖子重载? Thank you in advance.先感谢您。

When you return an action using RedirectToAction , the server returns a 3xx Response to browser with Location header to that Url.当您使用RedirectToAction返回一个操作时,服务器会向浏览器返回一个3xx 响应Location为 header 到该 Url。 Browser then proceeds to access that URL with GET verb, there is no way to change this unless you use Javascript to submit a form from that GET Url.然后浏览器继续使用GET动词访问 URL,除非您使用 Javascript 从该GET Url 提交表单,否则无法更改此设置。

As for your question, I think a good solution should separation of concern by having a Service for processing:至于您的问题,我认为一个好的解决方案应该通过服务进行处理来分离关注点:


[Route("api")]
public class ApiController
{

    ApiService service;
    public ApiController(ApiService service)
    {
        this.service = service;
    }

    [HttpGet, Route("")]
    public async Task OnGetAsync()
    {
        await this.service.DoSomethingAsync();
    }

    [HttpPost, Route("")]
    public async Task<IActionResult> OnPostAsync()
    {
        await this.service.DoSomethingElseAsync();

        // If you call this, don't redirect or it may call DoSomethingAsync twice
        await this.service.DoSomethingAsync();
        
        return this.RedirectToAction(nameof("OnGetAsync"));
    }

}

A quick, maybe dirty(?), solution.一个快速的,也许是肮脏的(?)解决方案。

[Route( nameof(RecreateWithId))]
public async Task<IActionResult> RecreateWithId(int id)
{
    var result = await _studentYearlyResultRepository.GetByIdAsync( id );
    var modelObj = new StudentYearlyResultReCreateVm() {StudentId = result.StudentId , AcademicYearId = result.AcademicYearId};

    return await Recreate(modelObj );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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