34409

ASP.NET 4.0 WebForms Routing Javascript not works

Question:

I'm try to use ASP.NET 4.0 WebForms Routing. Here is my RegisterRoutes function:

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"); }

For pages like /catalog/group/{group} everything is ok. But there are problems with pages catalog/good/{good}.

First strange thing: when I open this page it calls twice. So I see what value comes for Page.RouteData.Values["good"]. First time in Page.RouteData.Values["good"] has the right value (my goodId) but the second time I got the string value "WebResource.axd"!

So my highslide JavaScript does not work on the page. And when I click on highslide image the page reloads instead of executing the JavaScript.

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

Answer1:

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 <a href="https://stackoverflow.com/users/54364/milkywayjoe" rel="nofollow">MilkyWayJoe</a> 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`

Recommend

  • Why browser URL cannot end with .com?
  • How to render shape to string?
  • Aliasing a URL inside a custom ASP MVC Route
  • Reference generic url parameter in AuthorizeAttribute
  • ggplot2 polygon world map centred with limits gives funny edges
  • HTTP Module intercept requests and breaks custom errors configuration
  • WordPress > setting permalink option via script buggy?
  • Django model inheritance, filtering models
  • Do I need to seed any random number generator before using EVP_PKEY_keygen of OpenSSL?
  • JqueryMobile Popup menu is not working
  • How can I set a binding to a Combox in a UserControl?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Highlight one bar in a series in highcharts?
  • DomPDF {PAGE_NUM} not on first page
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Problems to linebreak with an int in JLabel
  • How to recover from a Spring Social ExpiredAuthorizationException
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • javascript inside java/jsp code
  • Can I make an Android app that runs a web view in Chrome 39?
  • htaccess rewriting URLs with multiple forward slashes
  • Convert array of 8 bytes to signed long in C++
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • PHP: When would you need the self:: keyword?
  • A cron job substitute?
  • Understanding cpu registers
  • coudnt use logback because of log4j
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How to get Windows thread pool to call class member function?
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices