0

I have an application in ASP MVC5 and while most works fine I encounter an issue when referencing javascript files, basically if the URL is directly to the controller like mypage.com/admin/products/create, the script gets called and works just fine, however, when calling a controller with and additional parameter like mypage.com/admin/products/edit/1525 then I have to add '../' to the javascript file reference or it won't load as well as any reference to images or paths within the javascript without the '../' I get a bunch of 404s. I believe I need to setup something in my routes but not sure, this is my routeconfig

    public static void RegisterRoutes(RouteCollection routes)
    {
        var namespaces = new[] {typeof (HomeController).Namespace} ;
        var AdminNamespaces = new[] {typeof (Areas.Admin.Controllers.HomeController).Namespace};

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


        routes.MapRoute("Home", "", new {controller = "Home", action = "Index"}, namespaces);
        routes.MapRoute("Login", "login", new { controller = "Login", action = "Index" });
        routes.MapRoute("Logout", "logout", new {controller = "Login", action = "Logout"});

    }

and this is the area registration

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "Admin_default",
            "admin/{controller}/{action}/{id}",
            new { action = "Index", controller = "Home", id = UrlParameter.Optional }
        );
    }

not sure what else could I include that would be useful

one particular script I am having issues is the nicedit text editor, and I have it on my bundle like this:

        bundles.Add(new ScriptBundle("~/scripts/nicedit")
            //.Include("~/scripts/lodash.js")
            .Include("~/scripts/nicedit.js")
            );

finally I call it on my view(not layout) cshtml just like this:

@Scripts.Render("~/Scripts/nicedit")
HectorDZJ
  • 43
  • 5

1 Answers1

-1

Look into JS and CSS bundling (available in .NET for some time now).

Be aware there have been issues in the past with CSS bundling breaking relative URLs for images (in the CSS). If that becomes an issue, look into CSS rewrite transforms (MVC Bundling and CSS relative URLs)

Community
  • 1
  • 1
dshapiro
  • 336
  • 1
  • 12