简体   繁体   English

ASP.NET WebForms路由Javascript错误

[英]ASP.NET WebForms Routing Javascript Error

I have some problems with JavaScript using ASP.NET 4.0 WebForms Routing. 使用ASP.NET 4.0 WebForms路由的JavaScript出现一些问题。

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) 但是当我使用它时,我在hte页面上出现了错误(在Firebug中)

Error: jQuery is not defined 错误:未定义jQuery

on this line: 在这条线上:

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

So my JavaScript does not work on the page that was routed. 因此,我的JavaScript在路由的页面上不起作用。

I added this line routes.Ignore("{resource}.axd/{*pathInfo}"); 我添加了以下路线: routes.Ignore("{resource}.axd/{*pathInfo}"); but it didn't helped me. 但这并没有帮助我。

I have solved my problem! 我已经解决了我的问题! The solution consists of 2 parts. 该解决方案包括2个部分。 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. 感谢MilkyWayJoe提出了该解决方案。

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 因此,我的网络资源在http://mysite.com/catalog/good/41页面上不再有路由

Also I have script events on the page like http://mysite.com/catalog/good/41/event/seq/1 . 我在页面上也有脚本事件,例如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: 并且不要忘记,必须在MapPageRoute声明之前放置您的Ignore声明:

routes.Ignore("catalog/good/{resource}.axd/{*pathInfo}");
routes.MapPageRoute("GoodInfo", "catalog/good/{good}", "~/GoodInfo.aspx");`enter code here`

If you look at the generated source of your page, is the jQuery library included? 如果查看页面的生成源,是否包括jQuery库?

If you are including jQuery via a resource, double check that it is included and that it is before that line that errors. 如果您通过资源包含jQuery,请仔细检查它是否包含在内,并且在该行之前出现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM