Ticket (Solved)

Anyone know of a clever URL Route override?

Hi guys, i've implemented RequireJS into my module, but due to Require's URL handling within Orchard - Require looks for any referenced javascripts in the wrong locality e.g. Depending on baseUrl, obviously:

Require looks here: /OrchardLocal/LearnReq/app.js When it should be: /OrchardLocal/Modules/LearnReq/scripts/app.js

...so seeing jtkech's response to a recent route question, I quickly implemented the following to get me going:

Routes:

new RouteDescriptor {
Priority = 100,
Route = new Route(
"LearnReq/start.js",
new RouteValueDictionary {
{"area", "LearnReq"},
{"controller", "Map"},
{"action", "start"},
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", "LearnReq"}
},
new MvcRouteHandler())
},

Controller:

[Themed(false)]
public ActionResult start()
{
var url = Url.Content("~/Modules/LearnReq/scripts/app/start.js");
return File(url, "text/javascript");
}

Obviously, this is a terrible hack - for every js I add or amend I have to then manually add/change all routes and references etc., = a maintenance nightmare!

Is there a way I can dynamically reroute such urls? Big thanks for any tips suggestions/solution, Liv

Re: Anyone know of a clever URL Route override?

Yes, you can generalize this kind of urls to serve files from different locations, file names...

E.g, to serve all js files from a directory, you can use something like this

            new RouteDescriptor {
                Priority = 1,
                Route = new Route(
                    "Evolutive.Automation/{fileName}",
                    new RouteValueDictionary {
                        {"area", "Evolutive.Automation"},
                        {"controller", "Script"},
                        {"action", "Index"},
                        {"fileName", UrlParameter.Optional}
                    },
                    new RouteValueDictionary(),
                    new RouteValueDictionary {
                        {"area", "Evolutive.Automation"}
                    },
                    new MvcRouteHandler()
                )
            }

    public ActionResult Index(string fileName) {

        var url = "~/Modules/Evolutive.Automation/Scripts/" + fileName;

        if (HostingEnvironment.VirtualPathProvider.FileExists(url)) {
            return File(url, "text/javascript");
        }
        else {
            return new HttpNotFoundResult();
        }
    }

Best

Friday, August 14, 2015 9:55:12 PM byjtkech
  • jtkech
  • Lv. 13 Rookie
  • Total EXP: 1028

Post a reply

You need to be signed in to post a reply.

Sign In