Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - Check that AngularJS $resource Request Server Side in .NET MVC

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

To have custom header in AngularJS just do this :

angular.module('myModule', [])

    .config(['$httpProvider', function($httpProvider) {

        $httpProvider.defaults.headers.common["FROM-ANGULAR"] = "true";

    }])

For use the X-Requested-With you have to do this too :

$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';

It's not set by default anymore because a lot part of the community have to delete this header to enable CORS request


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...