wdavo
wdavo

Reputation: 5110

Wrong script url being requested

I'm running Nancy using Self Hosting / OWIN and the Razor view engine

Specifically:

Nancy 0.21.1
Nancy.ViewEngines.Razor 0.21.1
Microsoft Owin.SelfHost 2.0.1

I have a html page with the following script reference

<script src="Scripts/jquery-1.6.4.min.js"></script>

If i make the following request (note the trailing slash)

http://localhost:3456/log/

The html page is correctly returned and the following script request is made

http://localhost:3456/log/Scripts/jquery-1.6.4.min.js

This is the correct behavior.

If i then make this request (note there is no trailing slash)

http://localhost:3456/log

The html page is correctly returned however a different script request is made.

http://localhost:3456/Scripts/jquery-1.6.4.min.js

This fails because it is the wrong URL. The script needs to be under /log

Nancy allows you to change the URL before it is handled, so I've tried adding the trailing slash if it is missing but this doesn't change anything (seems like a hack anyway)

Preferably I'd like to get it working without having to worry about the trailing slash. Failing that I guess I'd need a way to modify the initial response so the http referrer header is returned with the trailing slash? It's not obvious how to do this though.

N.B. My setup isn't a standard Nancy one. There's some custom static content conventions and view location conventions being used to get it to work how I want it to. Everything works correctly except when the trailing slash is missing.

This thread seems to talk about the same problem but for ASP.NET

http://forums.asp.net/t/1897093.aspx?Trailing+Slash+Nightmare

UPDATE 1

As suggested by @AndreD, using

/log/Scripts/jquery-1.6.4.min.js

Does work for both cases however "log" is a variable prefix and can not be hard-coded. A html helper may be an option and this is what I'm currently looking at the solve the problem

Upvotes: 4

Views: 709

Answers (1)

wdavo
wdavo

Reputation: 5110

So the solution was to create a helper which creates the correct script path

/log/Scripts/jquery-1.6.4.min.js

Helper class extensions

*SomeNamespace*

public static class UrlHelpers
{
  public static string Script<TModel>(this Nancy.ViewEngines.Razor.UrlHelpers<TModel> Self,     string Script)
  {
    var rootPath = Self.RenderContext.Context.Request.Path.TrimEnd('/');
    var scriptPath = string.Format("{0}/Scripts/{1}", rootPath, Script);
    return scriptPath;
   }
}

View:

@using SomeNamespace
@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase
  ...
  ...

  <script [email protected]("jquery-1.6.4.min.js")></script>
  <script [email protected]("jquery.signalR-2.0.0.min.js")></script>

Upvotes: 1

Related Questions