使用 ASP.NET MVC 和 JQuery 表单插件/文件上传检测 IsAjaxRequest() [英] Detecting IsAjaxRequest() with ASP.NET MVC and JQuery Form Plugin / File Upload

查看:29
本文介绍了使用 ASP.NET MVC 和 JQuery 表单插件/文件上传检测 IsAjaxRequest()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JQuery 表单插件在 ASP.NET MVC 应用程序上上传文件.我了解到,由于 iframe 用于文件上传(而不是 XMLHttpRequest,这是不可能的),因此 IsAjaxRequest 的服务器端检查失败.

I'm using the JQuery Form plugin to do a file upload on an ASP.NET MVC application. I've learned that since an iframe is used for file uploads (rather than XMLHttpRequest, which isn't possible), the server-side check for IsAjaxRequest fails.

我看过一些与此问题相关的帖子,但没有找到任何好的解决方案来解决此问题.与我的应用程序的其余部分一样,我希望能够同时支持启用 JavaScript 和禁用 JavaScript 的场景,这就是我想要检测请求是否为 ajax 的原因.

I've seen a few posts related to this question but haven't come across any good solutions to work around this issue. As with the rest of my application, I'd like to be able to support both JavaScript enabled and JavaScript disabled scenarios, which is why I'd like to detect whether a request is ajax or not.

我意识到所使用的 iframe 方法在技术上不是 ajax,但我正在尝试模仿 ajax 效果.

I realize that the iframe approach being used is not technically ajax, but I'm trying to mimic an ajax effect.

欢迎提出任何建议.

推荐答案

我刚刚意识到我完全没有回答这个问题,所以我在这里添加到顶部,并在下面留下我的旧答案:

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

问题是在 iFrame 上发布文件时,未设置X-Requested-With"标头,并且您无法在 Javascript 中为普通表单 POST 设置特定的请求标头.您将不得不求助于其他技巧,例如使用包含值的 POST 发送隐藏字段,然后更改或覆盖IsAjaxRequest"扩展方法以检查此条件.如何覆盖现有的扩展方法?

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?

可能最好的选择可能是包含您自己的具有不同名称的扩展方法,基于默认的 MVC 扩展方法代码和更改以检测您的 iFrame 上传 POST,然后在您希望的任何地方使用您的扩展方法需要它.

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 实际上默认将 'X-Requested-With' 标头设置为 'XMLHttpRequest'.只要您小心地通过 jQuery 执行所有 AJAX 调用,它就非常有用.

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; }
}

ActionFilterAttribute:

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;
            }
        }
    }
}

并使用实现:

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

          return View();
    }
}

这篇关于使用 ASP.NET MVC 和 JQuery 表单插件/文件上传检测 IsAjaxRequest()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆