jQuery ajax calls not working with ASP.Net Web Forms when FriendlyUrls are added

后端 未结 3 1944
孤街浪徒
孤街浪徒 2021-01-05 13:49

The following code works just fine without FriendlyUrls turned on for an ASP.Net Web Forms project:



        
相关标签:
3条回答
  • 2021-01-05 13:53

    After spending way too much time on this, I found out that I needed to change this in App_Start/RouteConfig.cs:

    //settings.AutoRedirectMode = RedirectMode.Permanent;
    settings.AutoRedirectMode = RedirectMode.Off;
    
    0 讨论(0)
  • 2021-01-05 14:01
    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            //Esta wea se deshabilita para que funcione ajax autocomplete.
            //settings.AutoRedirectMode = RedirectMode.Permanent;
            settings.AutoRedirectMode = RedirectMode.Off;
            routes.EnableFriendlyUrls(settings);
        }
    }
    
    0 讨论(0)
  • 2021-01-05 14:14

    So, ultimately I got the solution to my question by making following changes to my project:

    1. Add FriednlyUrls to the project.

    2. Remove the line in RegisterRoutes method that sets settings.AutoRedirectMode property in App_Start/RouteConfig.cs

      (note that setting it to RedirectMode.Permanent or RedirectMode.Off did NOT work for me)

    3. Add authorization in web.config as follows under system.web section

      <authorization>
          <allow users="*" />
      </authorization>
      
    4. Modify the url in ajax call set up to use Microsoft.AspNet.FriendlyUrls.Resolve function in order to get the correct url as below:

      <script type="text/javascript">
         $(document).ready(function () {
             $.ajax({
                url: '<%=Microsoft.AspNet.FriendlyUrls.FriendlyUrl.Resolve("/Default.aspx/GetData")%>',
                type: 'POST',                
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (result) {
                   var resultData = (result.d? result.d : result);
                   alert(resultData);
                },
                error : function(){
                   alert('error');
                }
         });
       });
      </script>
      
    0 讨论(0)
提交回复
热议问题