【问题标题】:ASP.NET Core API - View is not displayed after redirect from Api ControllerASP.NET Core API - 从 Api 控制器重定向后不显示视图
【发布时间】:2019-04-28 14:46:26
【问题描述】:

我在从 Api Controller 重定向到 Controller 时遇到了一些麻烦。我可以在邮递员的回复中看到,返回了正确的视图。

public class ApiController : Controller
{
    [HttpPost("/someApiAction/{id}")]
    public async Task<IActionResult> SomeApiAction(string id)
    {
        return RedirectToAction("Action", "Other", new { id = id});
    }
}

其他控制器:

public class OtherController : Controller
{
    public async Task<IActionResult> Action(string id)
    {   
        var model = new Model();
        return View(model);
    }
}

代码到达 return View(model) 但它没有显示。

有人可以帮忙吗?

【问题讨论】:

  • 重定向中的控制器名称应该是Other,而不是Controller
  • 已更正。无论如何,事实并非如此。
  • @Xardas 正确的观点是指源内容(即 html)或显示的 URL?
  • 显示的代码也准确吗?您有没有等待的异步操作。
  • 我们需要查看更多信息才能真正提供帮助。你真的对“Views/Other/Action.cshtml”有看法吗?

标签: c# .net asp.net-web-api model-view-controller asp.net-core


【解决方案1】:

我假设您是从 PostMan 而不是 Html 页面请求 Web api 控制器。要查看来自Postman的页面,您需要将内容复制出来并通过view html在线站点从浏览器中查看。

对于Postman,它将无法从响应中打开网络浏览器。

尝试通过请求api控制器来检查html,如下代码:

<form asp-controller="UserApi" asp-action="SomeApiAction" asp-route-id="2" formmethod="post">
    <button type="submit">Request Api</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2021-12-25
    • 2022-07-09
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多