如何在 .net 核心网络中将 url 设置为 webapi?

How to set url as webapi in the .net core web?

在 Web 应用程序中我创建了一个 Page:DetailPage

public void OnGet(int id)
{
    var bll = new BLL.Articles();
    Item = bll.GetModel(id);
    if (Item == null)
    {
        RedirectToPage("Blogs");
    }
}

URL 是

localhost/Detail?id=1

我可以做成web吗API URL比如`

localhost/Detail/1

我添加了 [HttpGet("id")] 但没有按预期工作。

如评论中的@Nkosi provided the proper document您必须修改Startup.cs

像这样更改 ConfigureServices 方法:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_0)
.AddRazorPagesOptions(options =>
{
    options.Conventions.AddPageRoute("/Detail", "Detail/{id}");
});

现在实施您的标准视图

@page
@model Item

<p>@Model.[property]</p>

您可以将其添加到 *.cshtml 页面的顶部:

@page "{id:int}"

然后在您的详细信息页面中:

public void OnGet([FromRoute] int id)

无需修改您的 Startup.cs 文件。