tags:

views:

22

answers:

2

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.

A: 

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

rob waminal
Thanks for your quick response! This is the solution.
Thomas
+1  A: 

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">

Hector
Url.Content("~/Foldername/Filename") is always a good practise to use when you include all kind of content and scripts.
QuBaR