Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 323 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to check if request is ajax or not in codebehind - ASP.NET Webforms

#1
I tried the `Request.IsAjaxRequest` but this does not exist in WebForms. I am making a JQuery ajax call. How do I check if this is a ajax request or not in C#?
Reply

#2
Generally, you will need to test for the `X-Requested-With` header, ensuring that its value is 'XMLHttpRequest'. I'm not a C# developer (yet), but a quick google search says that in C# it goes something like this:

Request.Headers["X-Requested-With"] == "XMLHttpRequest";
Reply

#3
Yes, `Request.IsAjaxRequest` looks at the headers and the querystring for `X-Requested-With`, but it seems your jquery isn't sending the `X-Requested-With` header.

You can try and see what headers it is sending by using Fiddler, or alternatively just send it in the querystring by setting the POST url to

`/whatever.aspx?x-requested-with=XMLHttpRequest`
Reply

#4
You could create your own extension method much like the one in the [MVC code][1]

E.g.


public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}

return (request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
}

HTHs,
Charles

**Edit:** Actually Callback requests are also ajax requests,

public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
var context = HttpContext.Current;
var isCallbackRequest = false;// callback requests are ajax requests
if (context != null && context.CurrentHandler != null && context.CurrentHandler is System.Web.UI.Page)
{
isCallbackRequest = ((System.Web.UI.Page)context.CurrentHandler).IsCallback;
}
return isCallbackRequest || (request["X-Requested-With"] == "XMLHttpRequest") || (request.Headers["X-Requested-With"] == "XMLHttpRequest");
}


[1]:

[To see links please register here]

Reply

#5
Try to check if the ScriptManager [IsInAsyncPostBack][1] :

ScriptManager.GetCurrent(Page).IsInAsyncPostBack


[1]:

[To see links please register here]

Reply

#6
Decorate your class with `[WebMethod(EnableSession = true)]`syntax like if you write the following function in code behind and call the same function from ajax call you will be sure.

[WebMethod(EnableSession = true)]
public static void getData(string JSONFirstData,string JSONSecondData, string JSONThirdData, string JSONForthData, ...)
{
//code
}

in Ajax URL be like `URL :'/Codebehind.aspx/getData'`
Reply

#7
I created an extension that I use:

internal static bool IsAjaxRequest(this HttpRequestMessage request)
{
return request != null && request.Headers.Any(h => h.Key.Equals("X-Requested-With", StringComparison.CurrentCultureIgnoreCase) &&
h.Value.Any(v => v.Equals("XMLHttpRequest", StringComparison.CurrentCultureIgnoreCase)));
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through