C # [WebMethod] misses when Content-Type '' application / Json is not provided

I created C # WebMethod

  [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static HttpResponseMessage GetPageHtml(string file, int page)
    {}

I can call it, and it works fine using Ajax, angular, Postman when I add the Content-Type: 'application / Json' header. Here is an example HTTP request that works fine.

$http({
            url: 'default.aspx/GetPageHtml?file=' + JSON.stringify(selectedFile) + '&page=' + itemNumber,
            method: "GET",
            headers: { 'Content-Type': 'application/json' },
            data:''
        }).then(function successCallback(response) {
            return response.d;
        }, function errorCallback(response) {

        });

But when I call the same method as for iframe ng-src, it does not work.

<iframe ng-src="default.aspx/GetPageHtml?file=candy.pdf&page=1" style="height: 150px;"></iframe>

I tested this Http call with the Google Chrome Developers tools and found that the Content-Type is "text / html", so it doesn't hit my WebMethod. See This Screenshot. Iframe ng-src

I knew a lot about Google and Stackoverflow, but I could not find the reason I needed to provide Content-Type: "application / json" to call my WebMethod

+4
1

, . WebForm GetDocumentHtml.aspx Page_Load , GetPageHtml WebMethod.

iframe ng-src URL

/GetDocumentHtml.aspx?file=calibre.docx&page=1

default.aspx/GetPageHtml?file=candy.pdf&page=1

Page_Load URL

var file = GetValueFromQueryString("file");
var page =Convert.ToInt32(GetValueFromQueryString("page"));

GetValueFromQueryString

 private String GetValueFromQueryString(String value)
    {
        try
        {
            return Request.QueryString[value].ToString();

        }
        catch (System.Exception exp)
        {
            return String.Empty;
        }
    }

, -. .

0

Source: https://habr.com/ru/post/1676067/


All Articles