user666
user666

Reputation: 318

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.

Upvotes: 1

Views: 509

Answers (2)

crgolden
crgolden

Reputation: 4634

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.

Upvotes: 2

Masoud Keshavarz
Masoud Keshavarz

Reputation: 2234

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>

Upvotes: 1

Related Questions