MVC View and Javascript URL issue.

Last post 04-13-2008 5:14 AM by srulyt. 10 replies.

Sort Posts:

  • MVC View and Javascript URL issue.

    04-10-2008, 7:04 PM
    • Loading...
    • kbach
    • Joined on 12-05-2006, 11:20 PM
    • Posts 13

    I have a strange issue that I hope someone can help me with.  I have an MVC project (Preview 2), setup with the default directory structure.  When I deploy to the IIS 6 webserver, my javascript files are not being found by the View.

    So, on the Index page, my <head> tag contains this:

    <script type="text/javascript" src="../../Content/pathTest.js" ></script>

     That works fine in VS2008 on my development machine, but when I deploy it to a Virtual directory on IIS 6, none of the javascript is found by the view.  The CSS files, which use the same "../../Content" structure are loading correctly, though.  Does anyone have a workaround, or know why this might be?

    Thanks.
     

    Vancouver Software Development / Business Automation Solutions

    PointyHat Software
    www.pointyhat.ca
  • Re: MVC View and Javascript URL issue.

    04-10-2008, 8:51 PM
    • Loading...
    • malkir
    • Joined on 01-09-2007, 10:05 PM
    • Posts 25

    I was going to tell you to do this:

     <script type="text/javascript" src="<%= Html.ResolveUrl("~/Content/pathTest.js") %>"></script>

     then I realized they made resolveurl internal for some reason.



    So here's the implementation I'm using:

    public static string ResolveUrl(this HtmlHelper helper, string virtualUrl)
            {
                HttpContext httpContext = System.Web.HttpContext.Current;
                string str = virtualUrl;
                if (!virtualUrl.StartsWith("~/", StringComparison.OrdinalIgnoreCase))
                {
                    return str;
                }
                virtualUrl = virtualUrl.Remove(0, 2);
                string applicationPath = httpContext.Request.ApplicationPath;
                if (string.IsNullOrEmpty(applicationPath) || !applicationPath.EndsWith("/"))
                {
                    applicationPath = applicationPath + "/";
                }

                return applicationPath + virtualUrl;
            }

     

    just put that in a static class in the System.Web.Mvc namespace. 

  • Re: MVC View and Javascript URL issue.

    04-10-2008, 9:16 PM
    • Loading...
    • ChanceUSC
    • Joined on 07-18-2007, 11:14 PM
    • Posts 183

    As long as you don't have a catchall route (or a catchall route without filters), you can use <head runat="server">. It will translate <a href="~/script/path/file.js"...> as normal and create the virtual path from the ~ wildcard.

     ..

     

  • Re: MVC View and Javascript URL issue.

    04-10-2008, 9:28 PM
    • Loading...
    • kbach
    • Joined on 12-05-2006, 11:20 PM
    • Posts 13

    Awesome malkir, worked like a charm!

     

    Vancouver Software Development / Business Automation Solutions

    PointyHat Software
    www.pointyhat.ca
  • Re: MVC View and Javascript URL issue.

    04-11-2008, 4:09 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 9:08 AM
    • Posts 584
    • ASPInsiders

    There's already a method to do that. Use don't need to create the ResolveUrl helper method!!!!

    <script type="text/javascript" src="<%= Url.Content("~/Content/pathTest.js") %>">

  • Re: MVC View and Javascript URL issue.

    04-11-2008, 8:18 AM
    • Loading...
    • ChanceUSC
    • Joined on 07-18-2007, 11:14 PM
    • Posts 183

     or just use a runat server :P

  • Re: MVC View and Javascript URL issue.

    04-12-2008, 12:34 AM
    • Loading...
    • pure.krome
    • Joined on 05-28-2006, 4:45 AM
    • Melbourne, Australia
    • Posts 242

    ChanceUSC:

     or just use a runat server :P

     

     

    eeks! aren't we trying to get away from runat=server ??? that's webform stuff, while this is all mvc .. right?

     

    +1 for Url.Content(..)

    :: Never underestimate the predictability of stupidity ::
  • Re: MVC View and Javascript URL issue.

    04-12-2008, 3:40 AM
    • Loading...
    • malkir
    • Joined on 01-09-2007, 10:05 PM
    • Posts 25

     Yeah, thanks for the headsup on Url.Content!

  • Re: MVC View and Javascript URL issue.

    04-12-2008, 7:22 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 9:08 AM
    • Posts 584
    • ASPInsiders

    pure.krome:
    eeks! aren't we trying to get away from runat=server ???
     

    Nope.

    Of course it's "WebForm stuff" but we're using the WebFormViewEngine so it's ok! They're just UserControls.

    It's only <form runat="server"> that is evil !!!

     

  • Re: MVC View and Javascript URL issue.

    04-12-2008, 9:47 PM
    • Loading...
    • ChanceUSC
    • Joined on 07-18-2007, 11:14 PM
    • Posts 183

    pure.krome:

    ChanceUSC:

     or just use a runat server :P

     

     

    eeks! aren't we trying to get away from runat=server ??? that's webform stuff, while this is all mvc .. right?

     

    +1 for Url.Content(..)

     

     

    When you add the attribute runat server, you are telling the engine to evaluate the contents and to perform some task before rendering the content. When you add runat="server" to a hyperlink, it will parse the attributes of the tag, find the href attribute, recognize the ~ symbol as something it needs to alter, and replace it with the virtual path. There is absolutely no reason not to utilize this when referencing static content files. Obviously this would be an issue when trying to use routing because the file doesn't exist and will confuse the server.

  • Re: MVC View and Javascript URL issue.

    04-13-2008, 5:14 AM
    • Loading...
    • srulyt
    • Joined on 02-02-2008, 1:16 PM
    • Posts 208

    the downside of runat=server is that it gets us back into control tree rendering, which if used to much will cause performance issues

    thats why we use divs instead of panels and spans instead of labels etc...

Page 1 of 1 (11 items)