0

Please note that I had already tried to apply the solution on Handling session timeout in ajax calls but did not worked for me.

In an ASP.NET MVC5 project, I open pages by rendering partialview via AJAX as shown below:

function renderPartial(e, controller, action) {

    var controllerName = controller;
    var actionName = action;

    if (String(actionName).trim() == '') {
        return false;
    }
    if (typeof (controllerName) == "undefined") {
        return false;
    }

    var url = "/" + controllerName + "/" + actionName;

    $.ajax({
        url: url,
        data: { /* additional parameters */ },
        cache: false,
        type: "POST",
        dataType: "html",
        error: function (jqXHR, textStatus, errorThrown) {
            var message = errorThrown;
            if (jqXHR.responseJSON != null) {
                message = jqXHR.responseJSON.message;
            }
        },
        success: function (data) {
            var requestedUrl = String(this.url).replace(/[&?]X-Requested-With=XMLHttpRequest/i, "");
            if (typeof (requestedUrl) == "undefined" || requestedUrl == 'undefined') {
                requestedUrl = window.location.href;
            }

            // if the url is the same, replace the state
            if (typeof (history.pushState) != "undefined") {
                if (window.location.href == requestedUrl) {
                    history.replaceState({ html: '' }, document.title, requestedUrl);
                }
                else {
                    history.pushState({ html: '' }, document.title, requestedUrl);
                }
            }
            //Load partialview
            $("#div-page-content").html(data);
        }
    });
};

On the other hand, when session timeout and call this method by a hyperlink, there is an empty white page on the partialview field where partialview page should be rendered. So, is it possible:

1) to display a partialview at this field?

2) to redirect user Login page?

Note: I would also be happy if you suggest a smart way to Handle Session Expire in ASP.NET MVC. Thanks in advance...

Community
  • 1
  • 1
Jack
  • 1
  • 21
  • 118
  • 236
  • Possible duplicate of [Handling session timeout in ajax calls](http://stackoverflow.com/questions/5238854/handling-session-timeout-in-ajax-calls) – CodingYoshi Dec 31 '16 at 21:22
  • @CodingYoshi No, never. Because I created a custom class as you mentioned above and decorate Controller. Then render partialview using AJAX. But whenever I render, the code hits MyAuthorizeAttribute and it returns "sorry, but you were logged out" message. Is there something I missed? – Jack Jan 04 '17 at 15:06
  • when you get the "sorry you were logged out" message, where does that come from? Is that another partial, or just a text response of some sort? MVC must be generating that. Also does it return a definable code, or a specific HTTP error code with it? If so, you can use that as a condition to either fetch another (unrestricted) partial which contains whatever info you want, or to cause a client-side redirect to the login page. – ADyson Jan 04 '17 at 15:50
  • sounds like it is better to be handled on the server... https://forums.asp.net/t/2041070.aspx?Redirect+to+login+Page+after+session+timeout+in+MVC+5, then just check if the response from the server in your ajax call is not a redirect – David Espino Jan 04 '17 at 17:22
  • @DavidEspino I have tried many examples as on [Redirect to login Page after session timeout in MVC 5](https://forums.asp.net/t/2041070.aspx?Redirect+to+login+Page+after+session+timeout+in+MVC+5) but it did not make any sense. Normally the code hits the implemented method, but after sesion expired the code does not hit neither Controller not the implemented method even if I remove [Authorize] attribute. Maybe it is mostly related to AJAX and after session is timeout AJAX might not pots request to Controller. If not, why the code does not hit to the Controller method? Any idea? Gracias – Jack Jan 05 '17 at 06:40
  • @ADyson It does not matter the message, because I just want to check the session timeout via an implemented class or AJAX call. I have tried bot, but did not solved the problem. SO, could you please simply post an example for both situations if you have any idea different from [Redirect to login Page after session timeout in MVC 5](https://forums.asp.net/t/2041070.aspx?Redirect+to+login+Page+after+session+timeout+in+MVC+5) or [jQuery ajax generic error handling and on a case-bycase basis](http://stackoverflow.com/questions/4887295/jquery-ajax-generic-error-handling-and-on-a-case-by-case-basis) – Jack Jan 05 '17 at 06:44
  • when you get the "sorry you were logged out" message, does it hit the "error" callback of your ajax? And what is the HTTP code returned? I'm guessing it will perhaps be 403 or even 401. – ADyson Jan 05 '17 at 09:30
  • It comes from the custom error class to the AJAX call (error part I think). But it does not matter, I just need a solution that can be via AJAX and can be independent from any other solution... – Jack Jan 05 '17 at 09:36

0 Answers0