Обнаружение IsAjaxRequest () с ASP.NET MVC и плагин формы jQuery / загрузка файла

Техника 1:

 DECLARE @p_date DATETIME
 SET     @p_date = CONVERT( DATETIME, '14 AUG 2008', 106 )

 SELECT  *
 FROM    table1
 WHERE   column_datetime >= @p_date
 AND     column_datetime < DATEADD(d, 1, @p_date)

преимущество этого состоит в том, что это будет использовать любой индекс на 'column_datetime', если это будет существовать.

9
задан goombaloon 5 November 2009 в 16:04
поделиться

2 ответа

I just realized I totally did not answer the question, so I am adding to the top here, and leaving my old answer below:

The problem is when posting a file over an iFrame, the "X-Requested-With" header is not set and you cannot set specific request headers for a normal form POST in Javascript. You'll have to resort to other tricks, like sending a hidden field with your POST that contains a value, and then change or override the "IsAjaxRequest" extension method to also check for this condition. How to override an existing extension method?

Probably the best option would probably be to include your own extension method with a different name, based off the default MVC extension method code with the changes to detect your iFrame upload POST, and then use your extension method anywhere where you expect to need it.


jQuery actually sets the 'X-Requested-With' header to 'XMLHttpRequest' by default. It is quite useful as long as you are careful to do all your AJAX calls over jQuery.

Depending on your needs, it's easy to setup the detection in an action filter to use it where needed, or even build it into a controller class like so:

[jQueryPartial]
public abstract class MyController : Controller
{
   public bool IsAjaxRequest { get; set; }
}

The ActionFilterAttribute:

public class jQueryPartial : ActionFilterAttribute  
{  
    public override void OnActionExecuting(ActionExecutingContext filterContext)  
    { 
        // Verify if a XMLHttpRequest is fired.  
        // This can be done by checking the X-Requested-With  
        // HTTP header.  
        MyController myController = filterContext.Controller as MyController;
        if (myController != null)
        {
            if (filterContext.HttpContext.Request.Headers["X-Requested-With"] != null
                && filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                myController.IsAjaxRequest = true;
            }
            else
            {
                myController.IsAjaxRequest = false;
            }
        }
    }
}

And using the implementation:

public class SomeController : MyController
{
    public ActionResult Index()
    {
          if (IsAjaxRequest)
               DoThis();
          else
               DoThat();

          return View();
    }
}
9
ответ дан 4 December 2019 в 06:57
поделиться

You need to set the "X-Requested-With" header for the IsAjaxRequest method to return true. Here's how you do it in jquery.

$(document).ready(function() {
     jQuery.ajaxSetup({
          beforeSend: function (xhr) {
                xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                return xhr;
          }
     });
});
11
ответ дан 4 December 2019 в 06:57
поделиться
Другие вопросы по тегам:

Похожие вопросы: