Reputation: 3
Intro
I'm developing a project with MVC.Net. I have just started a default website with a Home Controller and an Index action. I browse to the view with 'Home/Index/1'
and everyting works fine.
Now I want to add an extra url parameter, so I've changed my global.asax and added a foo parameter:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}/{foo}", // URL with parameters
new {controller = "Home", action = "Index", id = UrlParameter.Optional, foo = UrlParameter.Optional} // Parameter defaults
);
On the page I also have a little bit of jquery. For example this script:
<script type="text/javascript">
$(document).ready(
function ()
{
});
</script>
Problem
But now when I browse to my page with 'Home/Index/1/1' I get a javascript error:
Microsoft JScript runtime error: Object expected
When I browse to the page with 'Home/Index/1'
everything works fine. Probably there's a problem with my url routing, but I have no clue what I'm doing wrong.
Upvotes: 0
Views: 495
Reputation: 26690
You can also use the following syntax to force the resolution of the path
<script src="<%: Url.Content("~/Scripts/jquery-1.4.1.js") %>" type="text/javascript">
Upvotes: 1
Reputation: 18419
The cause of the problem is your Script source location. I assume your script source is ../../Scripts/jquery.js
which this is always being created by the application when you attach a js file in your page.
Explained.
js file mapped in `../../Scripts/jquery.js`
Page is `Home/Index/1/1`
js real content is placed in `/Scripts`
when the parser looks for the js it looks in `/Home/Scripts`
which is not where it where it is. Since ../.. = /Home in Home/Index/1/1
My Suggestion is
<%
string baseUrl = "http://" + Request.Url.Host + (Request.Url.Port != 80 ? ":" + Request.Url.Port.ToString() : "");
$>
<script type="text/javascript" language="javascript" src="<%=baseUrl %>/Scripts/jquery-1.4.1.js"></script>
This also goes to your CSS files included in your page. But for CSS links you don't put "
in your href
attribute of the <link>
tag
Related to my answer in here
Upvotes: 0