使用 ApiController 返回视图

问题描述 投票:0回答:1

我有一个

ApiController
,我想在
ApiController
中运行操作时显示视图。

public class PaymentController : ApiController
{
    [System.Web.Mvc.HttpPost]
    public async Task<IHttpActionResult> Success(JObject data)
    {
        // show result in a view 
        // return View("Result",paymentVM);
    }
}

我只能使用

ApiController
作为成功方法。

asp.net-mvc asp.net-apicontroller
1个回答
0
投票

ApiController 专门用于返回数据的 API 方法,而不是视图。如果您需要返回视图 - 请改用常规 MVC 控制器。

从 Controller 派生类,在 View 文件夹中创建一个名为(Sucess.cshtml)的视图,然后调用 return View 或 RedirectToAction

public class PaymentController : Controller
{

    [HttpPost]
    public ActionResult Success
    {
        return View();
        // return RedirectToAction("Index");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.