Q:

Generic string router with DB in Asp.net Core

I am creating an internet store.  And I want to add short URLs for products, categories and so on. 
For example:

store.com/iphone-7-plus

This link should open the page with iPhone 7 plus product. 
The logic is:

The server receives an URL
The server try it against existent routes
If there is no any route for this path - the server looks at a DB and try to find a product or category with such title. 

Obvious solutions and why are they not applicable:
The first solution is a new route like that:
public class StringRouter : IRouter
{
    private readonly IRouter _defaultRouter;

    public StringRouter(IRouter defaultRouter)
    {
        _defaultRouter = defaultRouter;
    }

    public async Task RouteAsync(RouteContext context)
    {
        // special loggic

        await _defaultRouter. RouteAsync(context);
    }

    public VirtualPathData GetVirtualPath(VirtualPathContext context)
    {
        return _defaultRouter. GetVirtualPath(context);
    }
}

The problem is I can't provide any access to my DB from StringRouter. 
The second solution is:
public class MasterController : Controller
{
    [Route("{path}")]
    public IActionResult Map(string path)
    {
        // some logic
    }
}

The problem is the server receive literally all callings like store.com/robots.txt
So the question is still open - could you please advise me some applicable solution? 

A:

For accessing DbContext, you could try :     
     using Microsoft. Extensions. DependencyInjection;

    public async Task RouteAsync(RouteContext context)
    {           
        var dbContext = context. HttpContext. RequestServices. GetRequiredService<RouterProContext>();
        var products = dbContext. Product. ToList();
        await _defaultRouter. RouteAsync(context);
    }

You also could try Middleware to check whether the reuqest is not exist, and then return the expected response.      
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
        {                
            app. Use(async (context,next) => {

                await next. Invoke();
                // add your own business logic to check this if statement
                if (context. Response. StatusCode == 404)
                {
                        var db = context. RequestServices. GetRequiredService<RouterProContext>();
                        var users = db. Users. ToList();
                        await context. Response. WriteAsync("Request From Middleware");
                }
            });
            //your rest code
        }

