c# - Reload routes on runtime in dotnetcore -


i have custom route reads urls no-sql database (mongodb) , add them route pipeline @ moment application starts, "pretty standard"

something (in startup.cs file):

app.usemvc(routes => {     routes.maproute(         "default",         "{controller=home}/{action=index}/{id?}");     routes.routes.add(         new landingpagerouter(routes, webrequest, memorycache, configuration));      // custom routes adds urls database } 

the issue if add route database after application has started 404 since routing system isn't aware of new route, think need add missing routes @ runtime or (less convenient) restart application pool web application (which has been developed on framework 4.5 , runs on different pool)

any other thoughts on this? thanks.

the first question database mean when say: i add route database , wether can keep routes in json, xml or ini file.

if can, example, keep routes in json file, there possible routes dynamically available on runtime (as can read in asp.net core documentation)

you can find full blog post here.

assuming routes.json json file structure similar following, , in same folder startup:

{     "route": "this route",     "another-route": "this route",     "default": "this default!" } 

you can configure startup class in following way:

note example not use mvc separate routing package, although assume can transpose work mvc.

public class startup {     public iconfiguration configuration {get;set;}     public startup(ihostingenvironment env)     {         var configurationbuilder = new configurationbuilder()             .setbasepath(directory.getcurrentdirectory())             .addjsonfile("routes.json", optional: false, reloadonchange: true);          configuration = configurationbuilder.build();     }     public void configureservices(iservicecollection services)     {         services.addrouting();     }     public void configure(iapplicationbuilder app)     {         var routebuilder = new routebuilder(app);          routebuilder.mapget("{route}", context =>          {             var routemessage = configuration.asenumerable()                 .firstordefault(r => r.key == context.getroutevalue("route")                 .tostring())                 .value;              var defaultmessage = configuration.asenumerable()                 .firstordefault(r => r.key == "default")                 .value;              var response = (routemessage != null) ? routemessage : defaultmessage;              return context.response.writeasync(response);         });          app.userouter(routebuilder.build());     } } 

at point, while application running, can modify json file, save it, because of reloadonchange: true parameter, framework reinject new configuration configuration property.

the implementation of reload based on file watcher, if want use database - sql server, think have implement yourself.

a (not pretty @ , reminded here completeness) solution create console application adds database changes in file, use file in application configuration.

best regards!


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -