检查.NET MVC是AngularJS $资源请求服务器端 [英] Check that AngularJS $resource Request Server Side in .NET MVC
问题描述
有没有办法判断一个请求是一个角度(1.1.5)$资源请求。我是pretty多找Request.IsAjaxRequest()这种类型的请求方法。
is there a way to tell if a request is an Angular (1.1.5) $resource request. I'm pretty much looking for a "Request.IsAjaxRequest()" method for this type of request.
我期待这是在被覆盖的AuthorizeAttribute的HandleUnauthorizedRequest我需要上下文结果集的一些JSON如果一个Ajax或角请求或者如果没有别的东西。
I'm looking this as in the HandleUnauthorizedRequest of an overriden AuthorizeAttribute I need to set the context result to some json if an Ajax or angular request or something else if not.
推荐答案
我不知道还有MVC3,但你可以设置自定义页眉从AngularJS所有请求。
I don't know well MVC3 but you can set a custom header for all request from AngularJS.
然后在服务器端,你只需要得到这个头,做你想要与角度的要求是什么。
Then on server side you just have to get this header and do what you want with request from angular.
要在AngularJS自定义标题只是这样做:
To have custom header in AngularJS just do this :
angular.module('myModule', [])
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common["FROM-ANGULAR"] = "true";
}])
有关使用的X请求,有了你必须这样做太:
For use the X-Requested-With you have to do this too :
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
这不是默认设置了,因为社会上的很多部分都删除这个头,让CORS请求
It's not set by default anymore because a lot part of the community have to delete this header to enable CORS request
这篇关于检查.NET MVC是AngularJS $资源请求服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!