I've got a fairly simple question for all the Razor experts out there. I'm trying to make a jQuery $.ajax() call to a URL, using Url.Content() to translate the home-relative path into an root-relative path. In so doing, Razor is getting a bit confused about where the end of my @section is located. I'd prefer to be able to specify the URL inline, but when I do that, Razor thinks that the end of the $.ajax() parameter list is the end of my @section. I'm using @section because I want to use layouts to place my javascript at the bottom of each file. Why is Razor getting so confused? I've even tried using @(Url.Content(...)), but that doesn't work either.
Also, is this the best way to approach the problem? I'm using the ASP.NET MVC 4 Preview.
This works:
@section Scripts {
<script type="text/javascript">
var getMessagesUrl = '@Url.Content("~/Logging/GetMessages")';
$(document).ready(function () {
$.ajax({
url: getMessagesUrl,
dataType: 'html',
success: function (result) {
$('tbody').html(result);
}
});
});
</script>
}
This doesn't:
@section Scripts {
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '@Url.Content("~/Logging/GetMessages")',
dataType: 'html',
success: function (result) {
$('tbody').html(result);
}
}); //Razor thinks this is the curly-brace that ends the section!
});
</script>
}
Seems to still be a problem with final MVC4 / Visual Studio 2010, but here is my fix:
Thanks for all of the help, folks.
It looks like it must have been a bug in the ASP.NET MVC 4 Preview. I just upgraded to the ASP.NET MVC 4 Beta which came out on February 15, and the problem is now completely gone.
Matthew's answer pretty much explains the behaviour (although, to be honest, I can't reproduce your problem - nor see why it wouldn't work - both your examples run just fine here). For a different approach, you could dedicate an action/view to generated javascript variables (urls, settings, localized texts, whatever), i.e.:
Globals.cshtml:
Yeah, this could have been a simple
Content()
result rather than a view - but when you have more globals (e.g. settings + urls + texts), you may want easier control over the script output and maybe serialize each dictionary individually. May also want to namespace that "GLOBALS" variable in some shared application namespace to avoid polluting the global scope.(e.g.) Index.cshtml:
... which simply includes the output from /Client/Globals. And "main.js", into which we have now moved the rest of your script:
main.js (static):
You can, of course, use the same kind of approach to output a few user/context/view-specific settings directly into the view. For a few URL's or data, the data-* attribute approach may be better depending on your tastes. I'm not a fan of stuffing tons of what's basically settings into attributes on every HTML page, though.
This is likely down to the behaviour of the parser. When it encounters the
@
symbol, the parser switches to code mode and will read the implicit expressionUrl.Content("~/Logging.GetMessages")
(well, actually it will read until the'
symbol, determine it is not a valid character in an expression and trackback to return until the end of the)
. It's after this stage that the parser is getting a little confused with your view because it's likely in code mode when it encounters the final}
character, and thinks it is the end of of a code span.The reality is, you have to be quite careful when using javascript within a razor view with C#. The transitions to code are explicit, e.g.
@
and after a{
, but the transitions to markup are a little harder to determine.Razor aside, my recommendation would be to stick your javascript application code in an external file, and take advantage of data-* attributes to convey meta information to your application code, e.g:
Which you can access as:
Update: Dave's less than symbol was not causing the problem, he only added it in his question for illustrative purposes.
On MVC4 I was able to isolate the issue. This would not compile:
But this would:
Seems like it was just the < in the comment that was throwing it.