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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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?
@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.
filterContext.HttpContext.Response.StatusCode = 401; even if I am doing this in java script i am getting status code as 500
@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.
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.