In ASP.NET MVC, adding a script reference throws exception - asp.net-mvc

On a skeleton ASP.MVC that Visual Studio creates, I add a script tag to my head section in Site.Master:
<script src="~/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
This causes the page to not render. In my case, I had a custom controllerfactory and the base method GetControllerInstance threw an exception:
The controller for path
'/~/Scripts/jquery-1.3.2.js' could not
be found or it does not implement
IController.
Using "../../Scripts/jquery-1.3.2.js" for the src does not work either.
The only way it works is:
<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.js") %>"
type="text/javascript"></script>
Then of course, the intellisense does not work for jquery. So I have to resort to adding the hack:
<% if (false) { %>
<script src="~/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
<% } %>
which the hotfix was supposed to fix according to ScottGu
A line above is a link to a stylesheet:
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
For some reason, that works fine. Whether I use the virtual or relative path, I can see that the resulting url on the page is "Content/Site.css". The same can't be said for the jquery url. jquery link is returned as is - the jquery url is returned on the page containing the "~" or the "../..".
Can someone tell me what is going on?
Thanks
UPDATE:
Thanks to the commenters, I remembered that ~ is an asp.net thing. My only question is then why doesn't the same issue exist for the stylesheet? The link tag above, for example, I can put ~ or relative paths and it always comes out right. Where is the magic?

Have you tried without the ~:
<script src="/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
The ~ character is used only by server side processing scripts indicating the root of the web site. The reason:
<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.js") %>" type="text/javascript"></script>
works is because it is translated to:
<script src="/Scripts/jquery-1.3.2.js" type="text/javascript"></script>

The "~" is not supported within standard HTML - it is an ASP.NET shortcut. So you've either got to do it the way you specified in your OP and the hack for intellisense, or as Darin specified but then you lose the ability to automatically pick up your VRoot.

I don't know for sure, but maybe MVC is smart enough to not include CSS files in the routing.

Related

CKEDITOR Problem on MVC2

I don't know how to use CKEditor in Mvc2 Framework.
I tried but it will not work
<%= Html.TextAreaFor(model => model.ABSTRACT) %>
<script type="text/javascript">
$(document).ready(function() {
$("$ABSTRACT").ckeditor();
});
</script>
Change the $ with # and it should definitely works
$("#ABSTRACT").ckeditor();
also be sure to have this 2 scripts referenced in your page.
<script type="text/javascript" src="#Content.Url("~/yourpath/ckeditor/ckeditor.js")"></script>
<script type="text/javascript" src="#Content.Url("~/yourpath/ckeditor/adapters/jquery.js")"></script>
You can also consider to associate ckEditor to a class instead of the Id of controls. so with only one call you can set the editor for all the control that have that class in your apps.
Is there any way you could just use the asp.net usercontrol for ckeditor?
http://ckeditor.com/download 3rd one down.
I have not tried it in mvc but I do not see why it would not work in MVC.

ASP.NET MVC: how to append a token to CSS URL properly?

I can append a token to my script references like this:
<script type="text/javascript" src="/Some.js?<%= Html.GetToken() %>"></script>
... and this works fine. But, if I try to append the same token the same way to a CSS file reference:
<link rel="stylesheet" type="text/css" href="/Some.css?<%= Html.GetToken() %>" />
... the IIS generates the following markup:
<link rel="stylesheet" type="text/css" href="/Some.css?<%= Html.GetToken() %>" />
I know I'm missing something super simple but I cannot see what exactly. Thanks in advance for any help!
This happens if your <head> section has runat="server". If you can live without it (in ASP.NET MVC you usually can) try it this way.
If not, add or modify the CSS link in your view's Page_Load. If that sounds awful to you, then removing runat="server" should be working :)
There a trick you can use for auto versioning.
Check out the post here:
Auto-versioning in ASP.NET MVC for CSS / JS Files?

JQueryUI DatePicker won't work in my ASP.NET MVC 2 application

I'm just trying to set up an input with datepicker and I'm getting the following error: "Object doesn't support this property or method".
I downloaded the JQueryUI without a theme and there is my head section of the master page:
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.4.1.min.js" type="text/javascript" />
<script src="/Scripts/jquery-ui-1.8.5.custom.min.js" type="text/javascript" />
<asp:ContentPlaceHolder ID="ScriptContent" runat="server" />
After that, I created an input text inside a partial view (that remains inside a view):
<input type="text" id="txtDataIda" />
And at the beggining of this partial view:
$(function() {
$("#txtDataIda").datepicker();
});
When I open the page, the error is shown and the datepicker won't work.
I suppose I must be doing something wrong since I've never got the chance to work with JQueryUI before.
Thanks in advance!
You should go to http://jqueryui.com/download and build a custom download with the features that you want. Then, just reference the 2 files (jquery-1.4.2.js, jquery-ui-1.8.5.custom.min.js) in the "js" folder and not all of the files in the "development-bundle" folder. Then, you don't have to worry about multiple files conflicting. I have a feeling that the .widget, .datepicker, and 1.8.custom files may be conflicting. But, if it's all in one custom file, then you don't have to worry about that.
Or if you're not using any other ui features, you could try referencing only the jquery-1.4.2.js, ui.core.js, ui.datepicker.js files to see if that works without conflict.
And, of course, double check that you've put the references in the correct master page that is being used.
Fixed!
The JQuery wasn't being loaded, because the script tag was self closing. When I changed it to , it worked, as bizarre as it seems.
Thanks!

AjaxHelper is not working

I am working on ASP.NET MVC.
in order to use AjaxHelper, I inserted two javascript files in the site.master as following.
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
then I used AjaxHelper in view pages, but it did not work.
so, I changed order of javascript file as following.
<script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
Then it worked :(
So, my question is that the order of JavaScript file effects to use AjaxHelper class?
Absolutely. The MVCAjax classes will need to reference the more generic Ajax classes. Therefore, the generic ones get referenced first
The MicrosoftMvcAjax.debug.jss file has code in it that references code in MicrosoftAjax.debug.js. Hence the order in which they are declared matters.

CSS and Javascript relative path confusion in ASP.NET MVC

My javascript paths work on this page:
http://localhost:53049/
But not on this page:
http://localhost:53049/Home/Messages
The reason is that the relative paths are different, the former requires ("js/...") and the latter requires ("../../js/...").
I'm including my javascript in my Site.Master file:
<script type="text/javascript" src="js/jquery.jqGrid.js"></script>
<script type="text/javascript" src="~/js/jquery.jqGrid.js"></script>
<script type="text/javascript" src="<%= this.ResolveClientUrl("~/Scripts/jquery-1.2.6.js") %>"></script>
How do I get around this relative path madness, i.e. what is the best practice way in ASP.NET MVC to set CSS/Javascript paths in the Site.Master so that they work for each view no matter how deep that view's URL happens to be.
ADDENDUM:
It seems that for the Index view, any path will work, strangely:
<script type="text/javascript" src="/Scripts/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../../Scripts/jquery-1.2.6.js"></script>
<script type="text/javascript" src="../../../Scripts/jquery-1.2.6.js"></script>
But for any other pages (pages with a deeper URL), none of these work.
What's going on here? How can we set the Javascript path once in Site.Master and they work for all pages?
ADDENUM II:
It turned out to only be a problem with the jqgrid javascript file (not the jquery file), apparently inside that file it is referencing other javascript files and gets confused:
<script type="text/javascript" src="<%= Url.Content ("~/js/jquery.jqGrid.js") %>"></script>
You can also use the Url.Content method call to make sure that the paths are correctly set.
Examples can be found here.
Regarding paths used in CSS documents:
/Content/site.css
Body {background-image:url('background.jpg');}
Relative paths in CSS documents are related to the CSS document, not the document being displayed.
This information saved me some headaches.
Try setting the javascript to use a forward slash at the beginning, like "/js/jquery.jqGrid.js" This will set them to use the root domain instead of relative pathing.
The solution for jqGrid: open the file jquery.jqGrid.js and locate the line:
var pathtojsfiles = "js/"; // need to be ajusted
As the comment says, you need to edit that path, e.g. for a typical ASP.NET MVC app,
var pathtojsfiles = "/Scripts/js/"; // need to be ajusted
You need to add runat="server" and to specify the absolute path like this:
<script type="text/javascript" runat="server" src="~/js/jquery.jqGrid.js"></script>]

Resources