Generic string router with DB in Asp.net Core









up vote
0
down vote

favorite












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:



  1. The server receives an URL

  2. The server try it against existent routes

  3. 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?










share|improve this question























  • Make route more specific for products, for example store.com/products/iphone-7-plus, then your last option will work without receiving "everything".
    – Fabio
    Nov 11 at 3:42










  • The problem is I already have an internet store worked on NotCommerce. So I already have URLs like store.com/iphone-7-plus in all search engines, marketplaces and so on.
    – Rustam Salakhutdinov
    Nov 11 at 6:34














up vote
0
down vote

favorite












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:



  1. The server receives an URL

  2. The server try it against existent routes

  3. 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?










share|improve this question























  • Make route more specific for products, for example store.com/products/iphone-7-plus, then your last option will work without receiving "everything".
    – Fabio
    Nov 11 at 3:42










  • The problem is I already have an internet store worked on NotCommerce. So I already have URLs like store.com/iphone-7-plus in all search engines, marketplaces and so on.
    – Rustam Salakhutdinov
    Nov 11 at 6:34












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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:



  1. The server receives an URL

  2. The server try it against existent routes

  3. 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?










share|improve this question















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:



  1. The server receives an URL

  2. The server try it against existent routes

  3. 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?







asp.net-core routing routes .net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 20:24

























asked Nov 10 at 19:53









Rustam Salakhutdinov

385117




385117











  • Make route more specific for products, for example store.com/products/iphone-7-plus, then your last option will work without receiving "everything".
    – Fabio
    Nov 11 at 3:42










  • The problem is I already have an internet store worked on NotCommerce. So I already have URLs like store.com/iphone-7-plus in all search engines, marketplaces and so on.
    – Rustam Salakhutdinov
    Nov 11 at 6:34
















  • Make route more specific for products, for example store.com/products/iphone-7-plus, then your last option will work without receiving "everything".
    – Fabio
    Nov 11 at 3:42










  • The problem is I already have an internet store worked on NotCommerce. So I already have URLs like store.com/iphone-7-plus in all search engines, marketplaces and so on.
    – Rustam Salakhutdinov
    Nov 11 at 6:34















Make route more specific for products, for example store.com/products/iphone-7-plus, then your last option will work without receiving "everything".
– Fabio
Nov 11 at 3:42




Make route more specific for products, for example store.com/products/iphone-7-plus, then your last option will work without receiving "everything".
– Fabio
Nov 11 at 3:42












The problem is I already have an internet store worked on NotCommerce. So I already have URLs like store.com/iphone-7-plus in all search engines, marketplaces and so on.
– Rustam Salakhutdinov
Nov 11 at 6:34




The problem is I already have an internet store worked on NotCommerce. So I already have URLs like store.com/iphone-7-plus in all search engines, marketplaces and so on.
– Rustam Salakhutdinov
Nov 11 at 6:34












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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






share|improve this answer




















  • Thanks! It works
    – Rustam Salakhutdinov
    Nov 18 at 19:12










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242834%2fgeneric-string-router-with-db-in-asp-net-core%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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






share|improve this answer




















  • Thanks! It works
    – Rustam Salakhutdinov
    Nov 18 at 19:12














up vote
1
down vote



accepted










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






share|improve this answer




















  • Thanks! It works
    – Rustam Salakhutdinov
    Nov 18 at 19:12












up vote
1
down vote



accepted







up vote
1
down vote



accepted






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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 6:11









Tao Zhou

3,77721026




3,77721026











  • Thanks! It works
    – Rustam Salakhutdinov
    Nov 18 at 19:12
















  • Thanks! It works
    – Rustam Salakhutdinov
    Nov 18 at 19:12















Thanks! It works
– Rustam Salakhutdinov
Nov 18 at 19:12




Thanks! It works
– Rustam Salakhutdinov
Nov 18 at 19:12

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242834%2fgeneric-string-router-with-db-in-asp-net-core%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線