简体   繁体   中英

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

In Web Application I created a 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 `

localhost/Detail/1

I added [HttpGet("id")] but is not working as expected.

You can just add this to the top of your *.cshtml pages:

@page "{id:int}"

Then in your details pages:

public void OnGet([FromRoute] int id)

No need to modify your Startup.cs file.

As @Nkosi provided the proper document in comments you must modify Startup.cs

Change ConfigureServices method like this:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM