要了解有關新的令人興奮的Asp.Net-5框架的更多信息,我正在使用最新發布的Visual Studio 2015 CTP-6來構建一個Web應用程序。
大多數事情看起來真的很有希望,但我似乎找不到Request.IsAjaxRequest() – 一個在舊的MVC項目中經常使用的功能。
有沒有更好的方法來做到這一點 – 這使得他們刪除這種方法 – 或者是“隱藏”在別的地方?
感謝任何建議,在哪里找到它或做什么改為!
我有點困惑,因為標題提到了MVC 5。
搜索Ajax in the MVC6 github repo doesn’t give any relevant results,但您可以自己添加擴展。從MVC5項目中進行的解壓縮代碼很簡單:
/// <summary> /// Determines whether the specified HTTP request is an AJAX request. /// </summary> /// /// <returns> /// true if the specified HTTP request is an AJAX request; otherwise, false. /// </returns> /// <param name="request">The HTTP request.</param><exception cref="T:System.ArgumentNullException">The <paramref name="request"/> parameter is null (Nothing in Visual Basic).</exception> public static bool IsAjaxRequest(this HttpRequestBase request) { if (request == null) throw new ArgumentNullException(nameof(request)); if (request["X-Requested-With"] == "XMLHttpRequest") return true; if (request.Headers != null) return request.Headers["X-Requested-With"] == "XMLHttpRequest"; return false; }
由於MVC6 Controller似乎使用Microsoft.AspNet.Http.HttpRequest,您必須通過對MVC5版本引入少量調整來檢查request.Headers collection是否適合標題:
/// <summary> /// Determines whether the specified HTTP request is an AJAX request. /// </summary> /// /// <returns> /// true if the specified HTTP request is an AJAX request; otherwise, false. /// </returns> /// <param name="request">The HTTP request.</param><exception cref="T:System.ArgumentNullException">The <paramref name="request"/> parameter is null (Nothing in Visual Basic).</exception> public static bool IsAjaxRequest(this HttpRequest request) { if (request == null) throw new ArgumentNullException("request"); if (request.Headers != null) return request.Headers["X-Requested-With"] == "XMLHttpRequest"; return false; }
或直接:
var isAjax = request.Headers["X-Requested-With"] == "XMLHttpRequest"
相關文章
- 1. asp.net-mvc – ASP.NET MVC RC中的Html.Image在哪里?
- 2. asp.net-mvc – 在哪里得到的Microsoft.Web.Mvc.dll
- 3. asp.net-mvc – 在哪里放置AutoMapper.CreateMaps?
- 4. asp.net-mvc-4 – ASP.NET MVC 4.0 RTM的符號在哪里?
- 5. asp.net-mvc – ASP.NET MVC – 在哪里拋出異常?
- 6. asp.net-mvc – asp.net mvc – 在哪里存儲userid – integer?
- 7. asp.net – System.Web.Security.MembershipProvider在哪里?
- 8. asp.net-core-mvc – ASP.NET Core MVC控制器在單獨的程序集中
- 9. C# – IoC和ASP.NET MVC,它在哪里開始?
- 10. asp.net-mvc – web.config在哪里用於MVC應用程序?
- 更多相關文章...
