【问题标题】:How can I return a razor page from a controller?如何从控制器返回剃须刀页面?
【发布时间】:2019-07-02 21:01:59
【问题描述】:

简单地做:

[HttpGet("{id}", Name = "Get")]
public ActionResult Get(int id)
{
    var result = new Pages.IndexModel().Page();
    result.ViewData["ID"] = id;
    return result;
}

给出The context used to execute 'context' must be an instance of 'Page'. Returning a 'PageResult' from a controller is a not supported.的错误

我只想将数据传递到页面并查看它。

【问题讨论】:

  • 您想使用两条路线显示同一个页面吗?从控制器返回页面有什么意义?您可以重定向到页面而不是返回它。
  • 我想根据url返回数据。就像有人去 /thread/54 它返回一个页面,其中包含来自数据库的数据,使用 54 进行查询

标签: asp.net-core razor controller


【解决方案1】:

我什至不需要控制器就知道了。

在.cshtml顶部添加@page "{id:int?}"

以后你可以做

<p>@RouteData.Values["id"].ToString()</p>

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 2020-12-20
    • 2020-03-02
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多