Verify that SQL Server MVC uses the URL query server.

is there any way to determine if the request is an Angular (1.1.5) $ resource request. I am very looking for the "Request.IsAjaxRequest ()" method for this type of request.

I view this as a HandleUnauthorizedRequest of a redundant authorized attribute. I need to set the context result for some json if the request is Ajax or Angular or something else if not.

+12
angularjs asp.net-mvc-3 request
Sep 25 '13 at 9:34 on
source share
2 answers

I don't know much about MVC3, but you can set a custom header for all requests from AngularJS.

Then, on the server side, you just need to get this header and do what you want with the request from angular.

To make the custom header in AngularJS just do this:

angular.module('myModule', []) .config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.common["FROM-ANGULAR"] = "true"; }]) 

To use X-Requested-With, you must do this too:

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

It is no longer installed by default because most of the community needs to remove this header in order to enable the CORS request.

+35
Sep 25 '13 at 9:52 on
source share
Answer to

@Thomas Pons is good and make AngularJS request standard with default settings on the Internet. But for a large application, with several angular modules, I find it cumbersome to install this for each module.

Not only that, but maybe you need to check whether to respond with a JSON response outside of an angular request. In this case, I prefer to check the Accept request header:

 var accepted = HttpContext.Request.Headers["Accept"].Split(','); if (accepted .Any(x => x == "application/json")) { // Send your JsonResult here } else { // Send your Html/Content result here } 
0
Dec 21 '15 at 15:45
source share



All Articles