Để có được ở tất cả các tuyến đường, bạn cần phải sử dụng phần ApiExplorer của MVC. Bạn có thể đánh dấu tất cả những hành động của bạn với một thuộc tính hoặc sử dụng một quy ước như thế này một:
public class ApiExplorerVisibilityEnabledConvention : IApplicationModelConvention
{
public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
if (controller.ApiExplorer.IsVisible == null)
{
controller.ApiExplorer.IsVisible = true;
controller.ApiExplorer.GroupName = controller.ControllerName;
}
}
}
}
Trong Startup.cs, thêm mới của bạn trong ConfigureServices(...)
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(
options =>
{
options.Conventions.Add(new ApiExplorerVisibilityEnabledConvention());
options.
}
}
Trong constructor ActionFilter
sau đó bạn có thể sử dụng của bạn tiêm để có được những ApiExplorer:
public class MyFilter : ActionFilterAttribute
{
private readonly IApiDescriptionGroupCollectionProvider descriptionProvider;
public MyFilter(IApiDescriptionGroupCollectionProvider descriptionProvider)
{
this.descriptionProvider = descriptionProvider;
}
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
base.OnActionExecuting(actionContext);
// The convention groups all actions for a controller into a description group
var actionGroups = descriptionProvider.ApiDescriptionGroups.Items;
// All the actions in the controller are given by
var apiDescription = actionGroup.First().Items.First();
// A route template for this action is
var routeTemplate = apiDescription.RelativePath
}
}
ApiDescription
, trong đó có các RelativePath
, đó là mẫu đường cho điều đó tuyến đường:
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Microsoft.AspNetCore.Mvc.ApiExplorer
{
public class ApiDescription
{
public string GroupName { get; set; }
public string HttpMethod { get; set; }
public IList<ApiParameterDescription> ParameterDescriptions { get; } = new List<ApiParameterDescription>();
public IDictionary<object, object> Properties { get; } = new Dictionary<object, object>();
public string RelativePath { get; set; }
public ModelMetadata ResponseModelMetadata { get; set; }
public Type ResponseType { get; set; }
public IList<ApiRequestFormat> SupportedRequestFormats { get; } = new List<ApiRequestFormat>();
public IList<ApiResponseFormat> SupportedResponseFormats { get; } = new List<ApiResponseFormat>();
}
}
Nguồn
2016-03-02 14:40:36
Ok - đó là từ điển trống trong trường hợp của tôi. Và RouteData dường như chứa dữ liệu liên quan đến tuyến đường hiện tại, không phải danh sách tất cả các tuyến đường. – clhereistian
'actionContext.RequestContext.Configuration.Routes' không tồn tại nữa. –