Implantando um IRouter personalizado no ASP.NET 5 (vNext) MVC 6

Estou tentando converteresta implementação de exemplo do RouteBase para trabalhar com o MVC 6. Eu trabalhei a maior parte seguindoo exemplo no projeto de roteamento, mas estou me perguntando como retornar o valor assíncronoTask do método Realmente não me importo se é realmente assíncrono (felicidades para quem pode fornecer essa resposta), por enquanto, só quero fazê-lo funcionar.

Tenho as rotas de saída funcionando (significandoActionLink funciona bem quando coloco os valores da rota). O problema está com oRouteAsync método.

public Task RouteAsync(RouteContext context)
{
    var requestPath = context.HttpContext.Request.Path.Value;

    if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
    {
        // Trim the leading slash
        requestPath = requestPath.Substring(1);
    }

    // Get the page that matches.
    var page = GetPageList()
        .Where(x => x.VirtualPath.Equals(requestPath))
        .FirstOrDefault();

    // If we got back a null value set, that means the URI did not match
    if (page != null)
    {
        var routeData = new RouteData();

        // This doesn't work
        //var routeData = new RouteData(context.RouteData);

        // This doesn't work
        //routeData.Routers.Add(this);

        // This doesn't work
        //routeData.Routers.Add(new MvcRouteHandler());

        // TODO: You might want to use the page object (from the database) to
        // get both the controller and action, and possibly even an area.
        // Alternatively, you could create a route for each table and hard-code
        // this information.
        routeData.Values["controller"] = "CustomPage";
        routeData.Values["action"] = "Details";

        // This will be the primary key of the database row.
        // It might be an integer or a GUID.
        routeData.Values["id"] = page.Id;

        context.RouteData = routeData;

        // When there is a match, the code executes to here
        context.IsHandled = true; 

        // This test works
        //await context.HttpContext.Response.WriteAsync("Hello there");

        // This doesn't work
        //return Task.FromResult(routeData);

        // This doesn't work
        //return Task.FromResult(context);
    }

    // This satisfies the return statement, but 
    // I'm not sure it is the right thing to return.
    return Task.FromResult(0);
}

Todo o método é executado até o fim, quando há uma correspondência. Mas quando termina a execução, não chama oDetails método doCustomPage controlador, como deveria. Acabei de receber uma página em branco no navegador.

Eu adicionei oWriteAsync linha como foi feito emesta postagem e escreveHello there para a página em branco, mas não consigo entender por que o MVC não está chamando meu controlador (nas versões anteriores isso funcionava sem problemas). Infelizmente, esse post abrangeu todas as partes do roteamento, exceto como implementar umIRouter ouINamedRouter.

Como posso fazer oRouteAsync função de método?

Implementação CustomRoute inteira
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class PageInfo
{
    // VirtualPath should not have a leading slash
    // example: events/conventions/mycon
    public string VirtualPath { get; set; }
    public int Id { get; set; }
}

public interface ICustomRoute : IRouter
{ }


public class CustomRoute : ICustomRoute
{
    private readonly IMemoryCache cache;
    private object synclock = new object();

    public CustomRoute(IMemoryCache cache)
    {
        this.cache = cache;
    }

    public Task RouteAsync(RouteContext context)
    {
        var requestPath = context.HttpContext.Request.Path.Value;

        if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
        {
            // Trim the leading slash
            requestPath = requestPath.Substring(1);
        }

        // Get the page that matches.
        var page = GetPageList()
            .Where(x => x.VirtualPath.Equals(requestPath))
            .FirstOrDefault();

        // If we got back a null value set, that means the URI did not match
        if (page != null)
        {
            var routeData = new RouteData();

            // TODO: You might want to use the page object (from the database) to
            // get both the controller and action, and possibly even an area.
            // Alternatively, you could create a route for each table and hard-code
            // this information.
            routeData.Values["controller"] = "CustomPage";
            routeData.Values["action"] = "Details";

            // This will be the primary key of the database row.
            // It might be an integer or a GUID.
            routeData.Values["id"] = page.Id;

            context.RouteData = routeData;
            context.IsHandled = true; 
        }

        return Task.FromResult(0);
    }

    public VirtualPathData GetVirtualPath(VirtualPathContext context)
    {
        VirtualPathData result = null;
        PageInfo page = null;

        // Get all of the pages from the cache.
        var pages = GetPageList();

        if (TryFindMatch(pages, context.Values, out page))
        {
            result = new VirtualPathData(this, page.VirtualPath);
            context.IsBound = true;
        }

        return result;
    }

    private bool TryFindMatch(IEnumerable<PageInfo> pages, IDictionary<string, object> values, out PageInfo page)
    {
        page = null;
        int id;
        object idObj;
        object controller;
        object action;

        if (!values.TryGetValue("id", out idObj))
        {
            return false;
        }

        id = Convert.ToInt32(idObj);
        values.TryGetValue("controller", out controller);
        values.TryGetValue("action", out action);

        // The logic here should be the inverse of the logic in 
        // GetRouteData(). So, we match the same controller, action, and id.
        // If we had additional route values there, we would take them all 
        // into consideration during this step.
        if (action.Equals("Details") && controller.Equals("CustomPage"))
        {
            page = pages
                .Where(x => x.Id.Equals(id))
                .FirstOrDefault();
            if (page != null)
            {
                return true;
            }
        }
        return false;
    }

    private IEnumerable<PageInfo> GetPageList()
    {
        string key = "__CustomPageList";
        IEnumerable<PageInfo> pages;

        // Only allow one thread to poplate the data
        if (!this.cache.TryGetValue(key, out pages))
        {
            lock (synclock)
            {
                if (!this.cache.TryGetValue(key, out pages))
                {
                    // TODO: Retrieve the list of PageInfo objects from the database here.
                    pages = new List<PageInfo>()
                    {
                        new PageInfo() { Id = 1, VirtualPath = "somecategory/somesubcategory/content1" },
                        new PageInfo() { Id = 2, VirtualPath = "somecategory/somesubcategory/content2" },
                        new PageInfo() { Id = 3, VirtualPath = "somecategory/somesubcategory/content3" }
                    };

                    this.cache.Set(key, pages,
                        new MemoryCacheEntryOptions()
                        {
                            Priority = CacheItemPriority.NeverRemove,
                            AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(15)
                        });
                }
            }
        }

        return pages;
    }
}
Registro DI CustomRoute
services.AddTransient<ICustomRoute, CustomRoute>();
Configuração de rota MVC
// Add MVC to the request pipeline.
app.UseMvc(routes =>
{
    routes.Routes.Add(routes.ServiceProvider.GetService<ICustomRoute>());

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

    // Uncomment the following line to add a route for porting Web API 2 controllers.
    // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
});

Caso isso importe, estou usandoBeta 5, DNX 4.5.1 eDNX Core 5.

Solução

Criei uma solução genérica que pode ser usada para uma chave primária simples para o mapeamento bidirecional de URLnesta resposta com base nas informações que aprendi aqui. O controlador, a ação, o provedor de dados e o tipo de dados da chave primária podem ser especificados ao conectá-los ao roteamento MVC 6.

questionAnswers(2)

yourAnswerToTheQuestion