Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Empty Urls with custom router

$
0
0

Hello everybody,

I'm going to extend the default template for the asp.net website in open source project on githubhttps://github.com/RichardMartens/Martens.Web.StarterKit. I want to extend a user management functionality and rapid dev module.

I want to speed up the development by creating a generic router, controller, view and viewmodel, I can parameterize with a DB Context. I develop a this generic router:

using Martens.Web.StarterKit.Controllers;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Abstractions;
using Microsoft.AspNet.Mvc.Controllers;
using Microsoft.AspNet.Mvc.Filters;
using Microsoft.AspNet.Mvc.Infrastructure;
using Microsoft.AspNet.Mvc.Routing;
using Microsoft.AspNet.Routing;
using Microsoft.Data.Entity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Martens.Web.StarterKit.Other
{
    public class EntityRouter<T> : IRouter where T : DbContext
    {
        public VirtualPathData GetVirtualPath(VirtualPathContext context)
        {
            VirtualPathData result = new VirtualPathData(this, context.Values["controller"] + "/" + context.Values["action"]);

            return result;
        }

        public async Task RouteAsync(RouteContext context)
        {
            string controller = context.RouteData.Values["controller"] as string,
                action = context.RouteData.Values["action"] as string;
            Type type;
            PropertyInfo[] properties;
            IEnumerable<PropertyInfo> properties2;
            MethodInfo method;

            if (controller == null || action == null)
            {
                return;
            }
            type = typeof(T);
            properties = type.GetProperties();

            if (properties == null)
            {
                return;
            }

            properties2 = properties.Where(w => w.Name == controller&& w.PropertyType.GetTypeInfo().IsGenericType&& w.PropertyType == typeof(DbSet<>).MakeGenericType(w.PropertyType.GenericTypeArguments));

            if (properties2 == null || properties2.Count() != 1)
            {
                return;
            }

            type = properties2.First().PropertyType;
            type = typeof(EntityController<,>).MakeGenericType(typeof(T), type.GetGenericArguments().First());

            var accessor = context.HttpContext.RequestServices.GetRequiredService<IActionContextAccessor>();

            method = type.GetMethod(action);
            if (method == null || method.IsPrivate)
            {
                return;
            }

            accessor.ActionContext = new ActionContext(context.HttpContext,
                                                        context.RouteData,
                                                        new ControllerActionDescriptor()
                                                        {
                                                            Name = action,
                                                            MethodInfo = method,
                                                            ControllerTypeInfo = type.GetTypeInfo(),
                                                            FilterDescriptors = new List<FilterDescriptor>(),
                                                            BoundProperties = new List<ParameterDescriptor>(),
                                                            Parameters = new List<ParameterDescriptor>(),
                                                            RouteConstraints = new List<RouteDataActionConstraint>(),
                                                        });

            var actionInvoker = context.HttpContext.RequestServices.GetRequiredService<IActionInvokerFactory>();
            var invoker = actionInvoker.CreateInvoker(accessor.ActionContext);

            await invoker.InvokeAsync();

            context.IsHandled = true;

            return;
        }
    }
}

The routing works well, but the links on the pages are broken.

This Markup:

<li><a asp-controller="Home" asp-action="Index">Home</a></li>

results this html:

<li><a href="">Home</a></li>

Has anybody an idea, what is wrong?

Thank you in advance.

Regards,

Richard Martens


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>