ASP.NET 4.0 WebForms Routing Javascript not works

浪尽此生 提交于 2019-12-11 19:27:36

问题


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?


回答1:


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`


来源:https://stackoverflow.com/questions/9503746/asp-net-4-0-webforms-routing-javascript-not-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!