且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

重写URL后,jQuery Ajax不调用webmethod

更新时间:2022-03-12 07:54:50

在ASP.NET Web表单中,当看起来像用于Web方法的链接(例如,在设置用于删除文件扩展名(例如,删除".aspx")的URL重写规则中,"/WebPageName.aspx/WebMethodName"未获豁免.因此,必须将以下条件添加到URL重写规则中,这看起来像是...

In ASP.NET Web Forms, this challenge arises when links that look like those used for web methods, e.g. "/WebPageName.aspx/WebMethodName", are not exempted in URL rewrite rules set up for removing file extensions (e.g. to remove ".aspx"). So, the following condition must be added to the URL rewrite rule and this can look like...

<rule name="removeAspxExtension" stopProcessing="true">
    <match url="(.*)\.aspx" />
    <conditions>
        <!--This is the condition to be added.-->
        <add input="{URL}" negate="true" pattern="(.*)\.aspx/(.*)" />
    </conditions>
    <action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>

此外,如果RouteConfig.cs中使用了RegisterRoutes(),则必须注释以下几行(以及所有不够具体的行,以至于它们可能影响看起来像"/WebPageName.aspx/WebMethodName"的链接)出来,如下...

Also, if RegisterRoutes() is being used in RouteConfig.cs, the following lines (and all that aren't specific enough such that they could affect links that look like "/WebPageName.aspx/WebMethodName") must be commented out too, as follows...

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // The commented code lines...
    //routes.MapRoute(
    //    name: "Default",
    //    url: "{controller}/{action}/{id}",
    //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    //);
}

现在,当从客户端访问网络方法时,例如通过使用jQuery Ajax,事情应该可以按预期进行.

Now, when a web method is accessed from the client-side, e.g. by using jQuery Ajax, things should work as expected.