1
$.ajax({
    type: "POST",
    url: "/Controller/Methods/" + $('#FrameworkId').val() + "/10/" + $('#category').val(),
    success: function(data) {
        alert("sucess")
    },
    error:function(data) {
        alert(data.statuscode)
    }
});

And in server side

suppose in server side In the action i am just doing

filterContext.HttpContext.Response.StatusCode = 401;
throw new UnauthorizedAccessException("Login_Expired");

Still alert(data.statuscode) gives 500

What can I do to get 401 as I set it?

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Kuttan Sujith
  • 7,889
  • 18
  • 64
  • 95

1 Answers1

2

Don't throw the exception

Don't throw the exception in your action filter.

What is the nature of you action filter anyway? Here's an example of an exception filter that returns a 400 back to the client.

Not directly related but just FYI. HttpUnauthorizedResult which is part of Asp.net MVC framework does this the same way:

public override void ExecuteResult(ControllerContext context)
{
    if (context == null)
    {
        throw new ArgumentNullException("context");
    }
    context.HttpContext.Response.StatusCode = 0x191;
}

As you can see it only returns an appropriate status code. Not exceptions thrown.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • I have done as the link provided by you. good, fine returning 401. to my ajax data.statuscode. great!.But now I am curios what the use of the code public override void ExecuteResult(ControllerContext context){...}. Would you plese tel this? – Kuttan Sujith Feb 10 '11 at 15:36
  • @user444569: I just wanted to point out that even Asp.net MVC doesn't do things the way you did. It was just like an assurance that not throwing is the correct way of doing it. That and nothing more. – Robert Koritnik Feb 10 '11 at 20:42
  • filterContext.HttpContext.Response.StatusCode = 401; even if I am doing this in java script i am getting status code as 500 – Kuttan Sujith Feb 11 '11 at 06:11
  • @user444569: So is it working or not? In the first comment you said it's now working, but then in this last one you say it isn't So is it working or not? Not throwing exception should work. – Robert Koritnik Feb 11 '11 at 08:31
  • if i am doing filterContext.HttpContext.Response.StatusCode = 400; then I am getting data.statuscode as 400. but when i set to 401 it goes to my log in page index action .but as it is a ajax request i get only the current page where i given the request ;so i given filterContext.ExceptionHandled = false; then i am getting 500 as status code in client side – Kuttan Sujith Feb 11 '11 at 08:54
  • @user444569: You mean you get Login form/view back as a result to your Ajax request? The thing is that 401 will be handled by authentication module so you better use some other code for your functionality. And when your session cookie expires (which is probably the reason for your 401) it would be better that you return some other code (that you know of) and redirect the whole page to a Login form. – Robert Koritnik Feb 11 '11 at 10:51
  • Actually as it is ajax request, even though it hit the log in controller function, the view is not rendered.I don't want the view to be rendered also.I just want to get the StatusCode we give at server side ,in client side also.I expect what we give what we get. I found a work around to the problem : see comment below – Kuttan Sujith Feb 11 '11 at 12:30
  • filterContext.HttpContext.Response.StatusCode = 400; ////The error section of Ajax will hit only if it is 400 filterContext.ExceptionHandled = true; filterContext.Result = new JsonResult { Data = new { success = false, statusCode = 401 }, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; – Kuttan Sujith Feb 11 '11 at 12:31
  • and in client side $.ajax({ ...... ......... error:function(response, status, error) { var response = $.parseJSON(response.responseText); if (response.statusCode == 401) { window.location=myloginpage; } } – Kuttan Sujith Feb 11 '11 at 12:36
  • @user444569: Of course your login page doesn't get displayed as it's an Ajax request. but if you'd check the response of that call in Firebug, you'd see that the actual response was the login form. Anyway. And as I mentioned previously, you are redirecting your page to a Logon page in JavaScript. No other way of doing it in Ajax request. But instead of setting a Json result with codes etc. you could just return a 30x HTTP code because you do want your user redirected. I do hope you realise you're having [401 issues because of forms authentication](http://stackoverflow.com/q/123726/75642). – Robert Koritnik Feb 11 '11 at 14:06