c# - How can I update an area route after the website is started? -


i trying update route table after application has started. doing in mvc area called "pages". website has custom urls each customer: www.mydomain.com/pages/companyname/

it works fine when start website. picks existing customers urls , can navigate them:

    public override void registerarea(arearegistrationcontext context)     {         context.routes.ignoreroute("{resource}.axd/{*pathinfo}");          list<customer> custs = new custbll().getactivecustomers();          string urllist = string.join("|", custs.select(c => c.urlidentifier).toarray());          context.maproute(             "pages_custurl", // route name             "pages/{custurl}/{controller}/{action}/{id}", // url parameters             new { controller = "home", action = "index", id = urlparameter.optional }, // parameter defaults             constraints: new { custurl = urllist }         );           context.maproute(             "pages_default",             "pages/{controller}/{action}/{id}",             new { controller = "home", action = "index", id = urlparameter.optional },             constraints: new { controller = new controllerconstraint() }         );      } 

but, when new customer created, can not url customer, "www.mydomain.com/pages/newcompany/", because 404 happen.

so tried add new function "updaterouteregistration()" called after new customer created.

    public static void updaterouteregistration()     {         routecollection routes = routetable.routes;         using (routes.getwritelock())         {             routes.remove(routetable.routes["pages_custurl"]);              list<customer> custs = new custbll().getactivecustomers();              string urllist = string.join("|", custs.select(c => c.urlidentifier).toarray());              routes.maproute(                 "pages_custurl", // route name                 "pages/{custurl}/{controller}/{action}/{id}", // url parameters                 new { controller = "home", action = "index", id = urlparameter.optional }, // parameter defaults                 constraints: new { custurl = urllist }             );          }     } 

the new function not work. there no errors when run but, afterwards cannot navigate anywhere. blankview error. seems corrupt routing table.

server error in '/' application.

the view 'blankview' or master not found or no view engine supports searched locations. following locations searched:
~/views/home/blankview.aspx
~/views/home/blankview.ascx
~/views/shared/blankview.aspx
~/views/shared/blankview.ascx
~/views/home/blankview.cshtml
~/views/home/blankview.vbhtml
~/views/shared/blankview.cshtml
~/views/shared/blankview.vbhtml
~/__mvcsitemapprovider/blankview.ascx

i using mvc 5.2.3. appreciated.

edit: wonder if need use arearegistraitioncontext. post, can access using reflection? get areas associated mvc project

i got working , wanted share may doing same thing or similar. not sure if best way happy works.

i did 2 things. one, saved arearegistrationcontext static member later use. two, did not try add/remove existing mapping. instead, added new one.

in case, new customer not added there not many mappings area.

here final class.

public class pagesarearegistration : arearegistration  {     public override string areaname      {                  {             return "pages";         }     }      public static arearegistrationcontext areacontext { get; set; }      public override void registerarea(arearegistrationcontext context)     {          context.routes.ignoreroute("{resource}.axd/{*pathinfo}");          // save context static later use         areacontext = context;          // customers         list<customer> custs = new custbll().getactivecustomers();          // list of domain urls         string urllist = string.join("|", custs.select(c => c.urlidentifier).toarray());          // map customer urls         context.maproute(             "pages_custurl", // route name             "pages/{custurl}/{controller}/{action}/{id}", // url parameters             new { controller = "home", action = "index", id = urlparameter.optional }, // parameter defaults             constraints: new { custurl = urllist }         );          // general area mapping         context.maproute(             "pages_default",             "pages/{controller}/{action}/{id}",             new { controller = "home", action = "index", id = urlparameter.optional },             constraints: new { controller = new controllerconstraint() }         );      }      public static void updaterouteregistration(string newurlid)     {         // context static member         routecollection routes = areacontext.routes;          // write lock avoid threading issues         using (routes.getwritelock())         {              // add new company url route table              areacontext.maproute(                 "pages_custurl_" + newurlid,                                      // route name                 "pages/{custurl}/{controller}/{action}/{id}",                       // url parameters                 new { controller = "home", action = "index", id = urlparameter.optional },  // parameter defaults                 constraints: new { custurl = newurlid }             );          }      }   } 

good luck if trying accomplish mvc area routing!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -