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:
- 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?
asp.net-core routing routes .net-core
add a comment |
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:
- 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?
asp.net-core routing routes .net-core
Make route more specific for products, for examplestore.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
add a comment |
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:
- 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?
asp.net-core routing routes .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?
asp.net-core routing routes .net-core
asp.net-core routing routes .net-core
edited Nov 10 at 20:24
asked Nov 10 at 19:53
Rustam Salakhutdinov
385117
385117
Make route more specific for products, for examplestore.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
add a comment |
Make route more specific for products, for examplestore.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
add a comment |
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
Thanks! It works
– Rustam Salakhutdinov
Nov 18 at 19:12
add a comment |
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
Thanks! It works
– Rustam Salakhutdinov
Nov 18 at 19:12
add a comment |
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
Thanks! It works
– Rustam Salakhutdinov
Nov 18 at 19:12
add a comment |
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
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
answered Nov 13 at 6:11
Tao Zhou
3,77721026
3,77721026
Thanks! It works
– Rustam Salakhutdinov
Nov 18 at 19:12
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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