0

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.

1
  • Can you edit your question and include the view code? It might help. Commented Oct 5, 2010 at 8:53

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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

1 Comment

Thanks for your quick response! This is the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.