2

I have some problems with JavaScript using ASP.NET 4.0 WebForms Routing.

My code:

void Application_Start(object sender, EventArgs e)
{
        RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{
    routes.Ignore("{resource}.axd/{*pathInfo}");             
    routes.MapPageRoute("GoodInfo", "catalog/good/{good}", "~/GoodInfo.aspx");
    routes.MapPageRoute("GoodGroup", "catalog/group/{group}", "~/default.aspx");
}

With no routing everything is ok. But when I use it I got an error on hte page (in Firebug)

Error: jQuery is not defined

on this line:

jQuery(document).ready(function () {
    HideBlocks();
});

So my JavaScript does not work on the page that was routed.

I added this line routes.Ignore("{resource}.axd/{*pathInfo}"); but it didn't helped me.

1
  • I posted solution here: solution Commented Mar 5, 2012 at 8:47

2 Answers 2

3

I have solved my problem! The solution consists of 2 parts. Firstly I changed my scripts definition from

<script type="text/javascript" src="../scripts/something.js"></script>

to

<script type="text/javascript" src="/../scripts/something.js"></script>

Thanks MilkyWayJoe fot that solution.

Secondly I added Ignore Routing

routes.Ignore("catalog/good/{resource}.axd/{*pathInfo}");

instead of:

routes.Ignore("{resource}.axd/{*pathInfo}");

So my web resources have no more routes on pages like http://mysite.com/catalog/good/41

Also I have script events on the page like http://mysite.com/catalog/good/41/event/seq/1. To catch all parameters I add to my route rules this

   routes.Ignore("catalog/good/{good}/{*query1}");
   routes.Ignore("catalog/good/{good}/{query1}/{*query2}");
   routes.Ignore("catalog/good/{good}/{query1}/{query2}/{*query3}");
   routes.Ignore("catalog/good/{good}/{query1}/{query2}/{query3}/{*query4}");

And don't forget that your Ignore declarations must be placed before MapPageRoute declarations:

routes.Ignore("catalog/good/{resource}.axd/{*pathInfo}");
routes.MapPageRoute("GoodInfo", "catalog/good/{good}", "~/GoodInfo.aspx");`enter code here`
Sign up to request clarification or add additional context in comments.

Comments

1

If you look at the generated source of your page, is the jQuery library included?

If you are including jQuery via a resource, double check that it is included and that it is before that line that errors.

1 Comment

Yes, jQuery included before this line. All works fine if I call page directly without routing.

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.