Maybe I can provide a useful work-around, depending on what you wish to accomplish.
I was tempted to find a way to evaluate razor expressions in a java script file. I wanted to attach a jQuery click event handler to submits with a specific class that are found on many pages. This should be done in a jQuery document ready event handler. This click event would perform an ajax call.
The url of these should be application relative, not absolute, in case the application lives below the root level. So, I wanted to use something like
$(document).ready(function () {
$('input[type="submit"].checkit)').click(function (e) {
$.ajax({
type: 'POST',
url: '@Url.Content("~/checkit")', //Razor expression here
dataType: 'json',
success: function (data) {
if (!data) {
e.preventDefault();
handleResponse(data);
}
},
data: null,
async: false
});
});
});
I solved this by wrapping the code in a function Checkit and moving the call to the layout view:
$(document).ready(function () {
Checkit('@Url.Content("~/checkit")');
});
Most of the javascript code is still in a javascript file.