1

I can create a file->new aspnetcore API project and use the IUrlHelper to generate a route by name without any issues.

[Route("api/[controller]")]
public class ValuesController : Controller
{
    public const string GetValues = "GetValues";
    public const string GetValuesById = "GetValuesById";
    public static string[] Values = new[] { "value1", "value2", "value3", };

    // GET api/values
    [HttpGet(Name = GetValues)]
    public IEnumerable<object> Get()
    {
        var result = new List<object>();
        for(int index = 0; index < Values.Length - 1; index++)
        {
            string routeForElement = this.Url.RouteUrl(GetValuesById, new { Id = index });
            result.Add(new { Value = Values[index], Route = routeForElement });
        }

        return result;
    }

    // GET api/values/5
    [HttpGet("{id}", Name = GetValuesById)]
    public string Get(int id)
    {
        if (id > (Values.Length - 1))
        {
            return "Invalid Id";
        }

        return Values[id];
    }
}

When the response is sent back, I correctly have the routes that I've created:

 [
  {
    "value": "value1",
    "route": "/api/v1/Values/0"
  },
  {
    "value": "value2",
    "route": "/api/v1/Values/1"
  },
  {
    "value": "value3",
    "route": "/api/v1/Values/2"
  }
]

I can then use the Visual Studio Scaffolding to create a Razor Page and continue to generate the same route without any issues within my Razor Page:

Model

public class IndexModel : PageModel
{
    public List<string> Routes { get; set; } = new List<string>();

    public void OnGet()
    {
        for (int index = 0; index < ValuesController.Values.Length; index++)
        {
            string routeForElement = this.Url.RouteUrl(ValuesController.GetValuesById, new { Id = index });
            Routes.Add(routeForElement);
        }
    }
}

Page

@page
@model UrlHelperWithPages.Pages.IndexModel
@foreach(string route in Model.Routes)
{
    <h4>@route</h4>
}

This renders the routes without issue.

If I add the aspnet-api-versioning nuget package and configure it's services:

services.AddApiVersioning();

My API controller continues to work with the following modification. Any request that is destined for this controller has the routes generated correctly.

[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ValuesController : Controller

However the Razor Pages stops working when we try to generate a route from within a Razor Pages request. The RouteUrl method now returns null. I've tried updating the route data provided to the RouteUrl method so that I pass a hard-coded version (for testing) and it doesn't work either.

new { version = 1, Id = index }

Is there any configuration that needs to happen on the api versioning package to support pages? We have razor pages that we want to generate API routes for rendering in the page, but it doesn't seem to work.

Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102
  • We're you ever able to solve this? There was a point where Razor Pages could not coexist with API versioning, but that was resolved month ago. In short, Razor Pages produce routes that do not follow the _normal_ conventions like other controllers, which resulted in no corresponding API version model. All such cases, Razor Pages, or otherwise are now ignored. – Chris Martinez Jan 11 '19 at 01:35
  • We abandoned Razor Pages for several reasons, so we never circled back to see if this was addressed by Microsoft ever. – Johnathon Sullinger Feb 13 '19 at 01:08

0 Answers0