asp.net mvc: how to detect when a page is called using ajax

后端 未结 6 1854
遥遥无期
遥遥无期 2021-02-12 20:26

how to detect when a page is called using ajax in asp.net mvc ?

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-12 21:04

    You can check it manually like this:

    bool isAjaxRequest = request.Headers["X-Requested-With"] == "XMLHttpRequest";
    

    Or when you're in a Controller in ASP.NET MVC, which references System.Web.Mvc you will get an extension-method on the HttpRequestBase object, which you can access within an ActionMethod like this:

    bool isAjaxRequest = Request.IsAjaxRequest();
    

提交回复
热议问题