2015-02-10 18 views
12

Trong ASP.NET Core, có cách nào để xem danh sách tất cả các tuyến được xác định trong Khởi động không? Chúng tôi đang sử dụng phương pháp mở rộng MapRoute của IRouteBuilder để xác định các tuyến đường.Lấy danh sách tất cả các tuyến đường

Chúng tôi đang di chuyển dự án WebAPI dự án cũ hơn. Ở đó chúng tôi có thể sử dụng GlobalConfiguration.Configuration.Routes để nhận tất cả các tuyến đường.

Cụ thể hơn, chúng tôi đang thực hiện việc này trong bộ lọc hành động.

public class MyFilter : ActionFilterAttribute 
{  
    public override void OnActionExecuting(ActionExecutingContext actionContext) 
    { 
     base.OnActionExecuting(actionContext); 

     // This no longer works 
     // var allRoutes = GlobalConfiguration.Configuration.Routes; 

     // var allRoutes = ??? 
    } 
} 

Trả lời

2

Bạn có thể nhận được một HttpRouteCollection từ HttpActionContext qua:

actionContext.RequestContext.Configuration.Routes 

RequestContext

HttpConfiguration

HttpRouteCollection

- Sau Câu hỏi cập nhật -

ActionExecutingContext có thuộc tính RouteData mà nó kế thừa từ ControllerContext, cho thấy thuộc tính DataTokens (là từ điển giá trị tuyến đường). Nó có lẽ không phải là bộ sưu tập mà bạn đang sử dụng để làm việc với, nhưng nó cung cấp quyền truy cập vào bộ sưu tập mà:

actionContext.RouteData.DataTokens 

DataTokens

+0

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

+0

'actionContext.RequestContext.Configuration.Routes' không tồn tại nữa. –

14

Để 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>(); 
    } 
} 
+0

Tôi ước tôi có thể upvote 100 lần này. Thật khó để tìm tài liệu về ApiExplorer trong AspNetCore và các mẫu trên github từ Microsoft đều lỗi thời. Cảm ơn bạn! – rrreee

+0

Chỉ cần cho rõ ràng, đây cũng là sự thay thế cho việc sử dụng 'RouteTable.Routes' toàn cầu trong MVC 5? Điều này có vẻ là một khủng khiếp rất nhiều để đi qua chỉ để liệt kê một danh sách các tuyến đường cho một ứng dụng; đặc biệt là vì các tuyến đường dễ dàng được thêm liên tiếp vào mã khởi động. –

+0

Tôi e rằng đó là câu hỏi của nhóm .NET. Đây là phương pháp được đề nghị với tôi một năm trước. –

Các vấn đề liên quan