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...

8
  • Possible duplicate of Handling session timeout in ajax calls Commented Dec 31, 2016 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? Commented Jan 4, 2017 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. Commented Jan 4, 2017 at 15:50
  • sounds like it is better to be handled on the server... forums.asp.net/t/…, then just check if the response from the server in your ajax call is not a redirect Commented Jan 4, 2017 at 17:22
  • @DavidEspino I have tried many examples as on 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 Commented Jan 5, 2017 at 6:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.