[英]How to set url as webapi in the .net core web?
In Web Application I created a Page:DetailPage 在Web应用程序中,我创建了一个Page:DetailPage
public void OnGet(int id)
{
var bll = new BLL.Articles();
Item = bll.GetModel(id);
if (Item == null)
{
RedirectToPage("Blogs");
}
}
The URL is 网址是
localhost/Detail?id=1
Can I make it as the web API URL such as ` 我可以将其作为Web API URL,例如`
localhost/Detail/1
I added [HttpGet("id")]
but is not working as expected. 我添加了
[HttpGet("id")]
但是没有按预期工作。
You can just add this to the top of your *.cshtml
pages: 您可以将它添加到
*.cshtml
页面的顶部:
@page "{id:int}"
Then in your details pages: 然后在您的详细信息页面:
public void OnGet([FromRoute] int id)
No need to modify your Startup.cs
file. 无需修改
Startup.cs
文件。
As @Nkosi provided the proper document in comments you must modify Startup.cs
由于@Nkosi在注释中提供了正确的文档 ,您必须修改
Startup.cs
Change ConfigureServices
method like this: 像这样更改
ConfigureServices
方法:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_0)
.AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Detail", "Detail/{id}");
});
Now implement your standard view 现在实现标准视图
@page
@model Item
<p>@Model.[property]</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.