ASP.NET MVC - What does IsAjaxRequest() actually mean?

后端 未结 2 1328
不知归路
不知归路 2020-12-10 10:37

I had previously created a method on my base controller:

public bool IsJsonRequest()
{
    var acceptTypes = Request.AcceptTypes;
    return acceptTypes != n         


        
2条回答
  •  粉色の甜心
    2020-12-10 11:07

    Specifically, the IsAjaxRequest code can be broken down to the function:

    public static bool IsAjaxRequest(this HttpRequestBase request)
    {
        if (request == null)
        {
            throw new ArgumentNullException("request");
        }
    
        return (request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
    }
    

    Edit - 21st January 2019

    I went back to my answer and found that my link to IsAjaxRequest is now broken. I've updated it with the current link but this is the AspNetWebStack repo and as such, is not the MVC v3 version of the code. That said, at time of looking, the code is still identical to what I wrote above.

提交回复
热议问题