locked
Web method fails after upgrading to .net 4.6.2 from 3.5 RRS feed

  • Question

  • User1168913849 posted

    My site was recently upgraded to .net 4.6.2 due to the server upgrade and I'm having a problem with jquery ajax.

    It returns an entire page instead of JSON string.

    It's posting a proper JSON string and return type is set to JSON but it stopped working after the upgrade. 

    It seems that .net 4.0 routing is causing this, but I can't narrow it down.

    I see it's posting to the right URL (with .aspx) but not getting JSON back. 

    What I'm missing here? 

    I'm keeping Webform and not planning to use friendly URL for now.

    Monday, January 21, 2019 8:00 PM

Answers

  • User1168913849 posted

    After wasting 2 days, trying all kinds of stuff, I decided to do the same procedure in a different machine. 

    The local set up worked fine and I compared the web.config file line by line.

    It turned out this line was causing the issue.

    <remove name="ScriptModule-4.0"/>

    It is under <g class="gr_ gr_307 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="307" data-gr-id="307">system</g>.webServer --> modules

    I have no idea what ScriptModule do but as soon as I commented out, it started working.

    I guess that the routing was not the issue for me.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 22, 2019 10:53 PM

All replies

  • User475983607 posted
    This symptom indicates an issue with the URL. Is there anyway you can post code that reproduces this issue? Can you explain your upgrade process? Did you make code or configuration changes? What troubleshooting steps have you performed?
    Monday, January 21, 2019 8:25 PM
  • User1168913849 posted

    I've used Visual Studio 2017 and changed the target <g class="gr_ gr_13 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="13" data-gr-id="13">frame work</g> version to 4.6.2.

    I have tried

    • adding <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> - still returns <g class="gr_ gr_14 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="14" data-gr-id="14">html</g> page with 200 code
    • adding ScriptModule in web.config - (incorrect web.config and couldn't open the site) Is this for asp.net Ajax?
    • tried to change RedirectMode.Off but no <g class="gr_ gr_15 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="15" data-gr-id="15">routeconfig</g> file
    • tried ResolveUrl in URL for ajax call but no change

    When I look at the console, it's posting to the right URL, but the page is not recognizing the method or something related...

    Here is the code. It may work in your environment since this is probably my set up issue.  

    <WebMethod()> Public Shared Function getitem(ByVal Id As String) As String
        Dim js As New System.Web.Script.Serialization.JavaScriptSerializer()
        Dim item As New products()
        'get detail based on the Id here
        item.imgid.Add(Id)
        item.imgttl.Add("book")           
        Dim str As String = js.Serialize(item)
        Return str
    End Function
    var dt = "{'Id':'abc'}";
    $.ajax({
        type: "POST",
        url: "item.aspx/getitem",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: dt,
        success: function(i) {
           buildpage(i.d)
        }
    });

    Monday, January 21, 2019 8:47 PM
  • User475983607 posted

    Working example...

    Public Module RouteConfig
        Sub RegisterRoutes(ByVal routes As RouteCollection)
            Dim settings As FriendlyUrlSettings = New FriendlyUrlSettings() With {
                .AutoRedirectMode = RedirectMode.Off
            }
            routes.EnableFriendlyUrls(settings)
        End Sub
    End Module

    Markup

    <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Item.aspx.vb" Inherits="VbWebApplication.Item" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
        <input id="Button1" type="button" value="button" />
        <div id="result">
        </div>
        <script>
            $('#Button1').click(function () {
                var dt = {'Id': 3};
                $.ajax({
                    type: "POST",
                    url: "/item.aspx/getitem",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify(dt),
                    success: function (i) {
                        console.log(i);
                    }
                });
            });
        </script>
    </asp:Content>

    Objects

    Public Class Product
        Public Property imgid As List(Of Integer)
        Public Property imgttl As List(Of String)
        Public Sub New()
            imgid = New List(Of Integer)
            imgttl = New List(Of String)
        End Sub
    End Class
    
    Public Class ProductRequest
        Public Property Id As Integer
    End Class

    Code behind

    Imports System.Web.Services
    
    
    Public Class Item
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        <WebMethod()> Public Shared Function Getitem(ByVal Id As Integer) As Product
            Dim item As New Product()
            item.imgid.Add(Id)
            item.imgttl.Add("book")
            Return item
        End Function
    
    End Class
    
    

    I was unable to exactly reproduce your code. I have no idea how the Product object is written.  I mocked a structure.

    On a side note, don't manually serialize objects in .NET let the framework handle the serialization.  It's easier. 


     

    Monday, January 21, 2019 11:44 PM
  • User1168913849 posted

    Thank you!

    Sorry, I didn't post the product object.

    I think I need to install FriendlyUrl package first so that I'll have RouteConfig class and the assembly, Microsoft.AspNet.FriendlyUrls

    >>On a side note, don't manually serialize objects in .NET let the framework handle the serialization.

    Does this apply to .NET 3.5 as well?

    Tuesday, January 22, 2019 12:01 AM
  • User1168913849 posted

    I'm still getting the same results.

    Installed Microsoft.AspNet.FriendlyUrls package and set RedirectMode.Off in RouteConfig class.

    I could confirm that the script works in a brand new Web form project.

    Hmmm. I'm stumped. Where do I go from here?

    Tuesday, January 22, 2019 1:01 AM
  • User475983607 posted

    Does this apply to .NET 3.5 as well?

    Yes, but your question does not have to do with the issue.

    Your method serializes the response twice.  First the .NET object is converted to a string and stored in the web server memory.  At this point there are two copies of the same thing in the server's memory the original object and the string representation of the object.   Next, the string is taken from server memory and sent down the wire which is serialization too.

    Tuesday, January 22, 2019 1:14 AM
  • User1168913849 posted

    Thank you for clarifying that!

    Yes, this is not related to my main issue...

    Tuesday, January 22, 2019 1:38 AM
  • User1168913849 posted

    After wasting 2 days, trying all kinds of stuff, I decided to do the same procedure in a different machine. 

    The local set up worked fine and I compared the web.config file line by line.

    It turned out this line was causing the issue.

    <remove name="ScriptModule-4.0"/>

    It is under <g class="gr_ gr_307 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="307" data-gr-id="307">system</g>.webServer --> modules

    I have no idea what ScriptModule do but as soon as I commented out, it started working.

    I guess that the routing was not the issue for me.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 22, 2019 10:53 PM