2

I've been developing a product for 2 years which is being sold by ourselves and partners. Now the manager wants to give the source code to our partners which will be only used as Demo apps with a Demo License.

All the demo module has been already developed but I need to show "Demo" on the route (url) when the license says so.

Example:

Every call to

/Controller1/Action1

Must transform the URL to

/Demo/Controller1/Action1

First try:

routes.MapRoute(
    name: "DefaultDemo",
    url: "Demo/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

However, this will partially work because I need to set "Demo" on every call to Controller/Action I've in my app.

Second try:

routes.MapRoute(
    name: "DefaultDemo",
    url: "{demo}/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    constraints: new { demo = "Demo" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, demo = "Demo" }
);

In this one, I tried to set it as a variable and "Demo" as a constraint of the that variable, to prevent someone to change it to other text.

Then I tried to use the "Default" route as Redirect to the other one, by setting the parameter "demo", however this obviously failed since it doesn't work this way.

Third try:

var demoRoute = routes.MapRoute(
    name: "DefaultDemo",
    url: "{demo}/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    constraints: new { demo = "Demo" }
);

routes.Redirect(x =>
    x.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional, demo = "Demo" })
)
.To(demoRoute);

In the last example I tried to use RouteMagic nugget to redirect the old "Default" route to the Demo one, but it didn't work.

Anyone who did something similar?

Leandro Soares
  • 2,902
  • 2
  • 27
  • 39
  • 2
    Your likely going to have more joy overridding the controller factory or adding a custom HTTP handler to do this rather than using the routing table. You need some lower level control. You then might also need the demo route but the re-direct is going to need to be performed at a lower level – Liam Oct 12 '16 at 11:14
  • I was trying not to do that and only use the routing table. But I'll give it a try. – Leandro Soares Oct 12 '16 at 11:51

0 Answers0