WebAPI redirect not working? - asp.net-web-api

I'm trying the following:
[System.Web.Http.AcceptVerbs("PUT")]
public HttpResponseMessage MakePost(PostDto post) {
try {
var response = Request.CreateResponse(HttpStatusCode.Redirect); // tried MOVED too
response.Headers.Location = new Uri("google.com");
return response;
} catch (Exception e) {
ErrorSignal.FromCurrentContext().Raise(e);
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
Which seems to be partially working - when this is called, I see the POST request in chrome debugger. Nothing appears in the Response tab, but then I see a GET request sent to the new URI, yet the page never changes, and my AJAX call throws an error:
var options = {
url: postUrl,
type: type,
dataType: 'json',
xhrFields: {
withCredentials: true
}
};
return $.ajax(options)
.done(function (response) {
// do stuff
})
.fail(function (response) {
alert('error) // this gets hit - shouldn't the browser have redirected at this point?
}).complete(function () {
// stuff
});
};
If I inspect response, I see a Status 200 "OK".... I'm so confused.
What am I doing wrong?

This happens because the code issuing the AJAX request follows the redirect, not the browser. This will then fail because the AJAX request tries to access a different domain. If you want to redirect the browser, you should return some JSON result or a custom HTTP header, manually pick this up in your jQuery, and do the redirect there.
In your controller:
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Headers.Add("FORCE_REDIRECT", "http://google.com");
Then add a success callback to your AJAX call:
success: function(data, textStatus, jqXHR) {
if (jqXHR.getResponseHeader('FORCE_REDIRECT') !== null){
window.location = jqXHR.getResponseHeader('FORCE_REDIRECT');
return;
}
}
In the past, I've wrapped the controller result up in a custom action result class for reuse.

Related

How to handle AJAX exception in ASP.NET Core MVC and show custom error page?

I have just begun to work with ASP.NET Core MVC and web development in general, and I am struggling to understand how to show an error page from an AJAX call.
What I would like to do is to show a custom page with the error message on if the Ajax call fails. So far I have the following code which takes me to my 500 page when I throw an exception in the controller but how do I get it to show the exceptions message on that page?
StartUp.cs middleware:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePagesWithReExecute("/Error/Error", "?Code={0}");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
Error controller:
public IActionResult Error(int? Code = null)
{
if (Code.HasValue)
{
if (Code.Value == 404 || Code.Value == 500)
{
var viewName = Code.ToString();
return View(viewName);
}
}
return View();
}
AJAX call:
// Use ajax call to post to the controller with the data
$.ajax({
type: "POST",
url: "/Renewals/GenerateQuotes",
data: { selectedContractIds: ids },
success: function (response) {
// Show success and refresh the page
Toast.fire({
icon: 'success',
title: 'Quotes Generated'
}).then(function () {
location.reload();
});
},
error: function (xhr, status, error) {
// Response message
var response = xhr.responseText;
window.location.href = '#Url.Action("Error", "Error")?Code=' + xhr.status;
}
})

How to redirect to error page from onexception method instead of returning to jQuery/Ajax call in c# mvc

I'm trying to redirect to an Error page inside onexception method when an exception occurs. But the thing is I have Ajax function, so even if I redirect inside onexception class, it does not redirect to error page and it always end up with executing Ajax function. Please can anyone suggest a solution for this.
This is my controller method and when exception throws, it will call to base controller on exception method.
public ActionResult DeleteConfirmed(int id)
{
try
{
string displayMessage = string.Empty;
//Delete ward details and check successfulness of the function and return json result
if (wardManager.DeleteWard(id) == true)
{
displayMessage = CustomEnumMessage.GetStringValue(ConfirmationMessages.ConfirmationErrorMsg);
return Json(new { Message = displayMessage });
}
//Return json result if unsuccessfull
else
{
displayMessage = CustomEnumMessage.GetStringValue(ConfirmationMessages.ConfirmationRemovedMsg);
return Json(new { Message = displayMessage });
}
}
catch (System.Exception ex)
{
throw ex;
}
}
This is my base controller onexception method
protected override void OnException(ExceptionContext filterContext)
{
//Get exception type
System.Type ExceptionType = filterContext.Exception.GetType();
if (ExceptionType.Name == "DbUpdateException")
{
ViewData["ErrorMessage"] = filterContext.Exception.InnerException.Message;
this.View("DatabaseException", filterContext.Exception).ExecuteResult(this.ControllerContext);
}
else
{
ViewData["ErrorMessage"] = filterContext.Exception.Message;
this.View("ApplicationException", filterContext.Exception).ExecuteResult(this.ControllerContext);
}
}
This is my Ajax function
$.ajax({
type: "POST",
dataType: "json",
jsonpCallback: "onJSONPLoad",
url: '#Url.Action("Delete")',
data: $('#form').serialize(),
success: function (data) {
$('#submit').hide();
TriggerMsg(data.Message);
},
error: function (xhr) {
if (xhr.getResponseHeader('Content-Type').indexOf('application/json') > -1) {
var json = $.parseJSON(xhr.responseText);
alert(json.errorMessage);
}
}
});
I built this Ajax function for only displaying Successful messages and it avoids me to redirecting to error page. The problem is even if i redirect to error page inside onexception method, finally fire into Ajax error: function and it does not redirect into DatabaseException or ApplicationException views. Can anyone suggest a solution for this issue.
Thanks
Your problem is that when you do a post from an ajax function, whatever result you have in your controller will always end up coming back to the success function.
What you want to do is return a url to your ajax function and from there redirect the user.
In your controller return the following
result = Json(new { redirect = Url.Action("ActionName", "ControllerName", new { area = "" }) });
Then in your javascript you will look for this redirect variable and set the window.location to it
success: function (data) {
if (data.redirect) {
window.location.href = data.redirect;
}
},
If you also want to pass a message then you can probably put it in the session such as Session["ErrorMessage"] = error from the controller and use it the same way in your error view

Bind event after login

I have made a filter called auth that check if user is logged. If is not logged it redirect on the main page but if is a call ajax? I just checked if is it. If it is i just send an json status "no-log". Now i received my json response "no-log" on my client and i would like open a modal for ask login and password. The solution that i thougth was put easily for each ajax request an if statement to check if the response status is "no-log" and show the function of modal. BUT OF COURSE is not good for future update, I'm looking for a good solution where i can bind this event and if i want on the future add other status. Any suggest?
Route::filter('auth', function()
{
if (Auth::guest()) {
if ( !Request::ajax() ) {
Session::put('loginRedirect', Request::url());
return Redirect::to('/');
} else {
$status = "no-log";
return json_encode(array('status' => $status));
}
}
});
A example of call ajax
$(document).on("click", ".delete", function() { // delete POST shared
var id_post = $(this);
bootbox.confirm("Are you sure do want delete?", function(result) {
if (result) {
$.ajax({
type: "POST",
url: '/delete_post/' + USER,
data: { id_post: id_post.attr('id') },
beforeSend: function(request) {
return request.setRequestHeader("X-CSRF-Token", $("meta[name='token']").attr('content'));
},
success: function(response) {
if (response.status == "success") {
id_post.parents('div.shared_box').fadeOut();
}
},
error: function(){
alert('error ajax');
}
});
} else {
console.log("close");
}
});
});
After 10 days of exploring an idea I found a way to override ajax comportment:
It just need you replace every $.ajax() by a custom one.
If I re-use your code:
$(document).on("click", ".delete", function() { // delete POST shared
var id_post = $(this);
bootbox.confirm("Are you sure do want delete?", function(result) {
if (result) {
myCustomAjax({ // In place of $.ajax({
type: "POST",
...
Then this custom function allow you to add some action before or after each ajax callback:
For instance checking the JSON return value in order to decide if I trigger the success callback or I show a warning:
function myCustomAjax(options) {
var temporaryVariable = options.success;
options.success = function (data, textStatus, jqXHR) {
// Here you can check jqXHR.responseText which contain your JSON reponse.
// And do whatever you want
// If everithing is OK you can also decide to continue with the previous succeed callback
if (typeof temporaryVariable === 'function')
temporaryVariable(data, textStatus, jqXHR);
};
return $.ajax(options);
}
If you return a 401 for all not loggedin requests, you can use $.ajaxSetup to handle all ajax errors in your application.
$.ajaxSetup({
error: function(jqXHR, exception) {
if (jqXHR.status == 401) {
window.location = 'your-login-page';
}
}
});

How do I call a method in Scala through javascript & ajax?

I do not know if my title was perhaps a little misleading. But here's what I really need help with.
I'm making a get on this url:
$.get("/fb/login/"+fbEmail, function(data){
console.log(data);
});
This is my routes:
GET /fb/login/:email presentation.controllers.Auth.authenticateSocialNetwork(email:String)
And here's my action:
def authenticateSocialNetwork(email:String) = Action {
if(!editorRepo.getEditorByEmail(email).isEmpty){
Redirect(routes.Profile.editorProfile).withSession(Security.username -> email)
} else {
Redirect(routes.Profile.initiatorProfile).withSession(Security.username -> email)
}
}
My expectation from this is that my action gets called and fires of what's inside it. In other words, Redirecting.
But what actually happens, which is not so illogical, is that my $.get call gets a response with my redirect's.
How do I actually call my action-method, without sending a response to javascript?
Here's my function in javascript, posting this snippet for it to be more clear in our discussion in the comments above.
function addClickToLoginButtons(){
$("#loginWithFb").click(function(){
FB.login(function(response){
if(response.authResponse){
FB.api('/me', function(response){
var fbEmail = response.email;
$.get("/fb/isRegisteredAtNetwork/"+fbEmail+"/facebook", function(data){
if(data == "true"){
if(confirm("Do you want to log with facebook-account "+fbEmail+"?")){
$.get("/fb/login/"+fbEmail, function(data){ *//HERE'S WHERE I WOULD WANT TO CALL MY METHOD IN SCALA*
console.log(data);
});
} else {
console.log("try again with a different facebook-account");
} //end confirm else
} else {
console.log("Logged in not in database");
}//end get else
});
});
} else {
console.log("permission not granted");
} // end authResponse else
}, {scope: 'email'});
});
}
In your action, instead of returning Redirect, return Ok(urlToBeRedirectedTo).withSession(...). Once this response received in the javascript code, do your stuff and then call window.location = urlToBeRedirectedTo;.
This will add the email to the session, and will redirect to the wanted URL.

Alert is coming before response

I have this ajax function which validates the user provided key. but the alert comes before the ajax response and due to which if the user provide a wrong key even can get access
$(document).ready(function() {
$('#submit').click(function(e) {
e.preventDefault();
var key = $('#downloadkey').val();
var dataString = {KEY:key};
$.ajax({
url: "/mediabox/home/validate_key",
type: 'POST',
data: dataString,
success: function(msg) {
if(msg=="true")
{
alert("do something");
}
else
{
alert("Your download key is either wrong or you didn't provide it.");
return false;
}
}
});
});
});
What makes you believe the alert is coming before the response? The success handler is only invoked after the response has been successfully received client-side.
To confirm, you can edit your success handler to log the response:
success: function(msg) {
console.log(msg);
if(msg=="true")
{
alert("do something");
}
else
{
alert("Your download key is either wrong or you didn't provide it.");
return false;
}
}
Also, if you're using the return false to deny access to the user by blocking the HTML action that, won't work due to the asynchronous nature of AJAX.
The success function is called when the request completes.
success(data, textStatus, jqXHR)Function, Array
A function to be called if the request succeeds. The function gets passed three
arguments: The data returned from the server, formatted according to
the dataType parameter; a string describing the status; and the jqXHR
(in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the
success setting can accept an array of functions. Each function will
be called in turn. This is an Ajax Event.
The code within the success handler will only execute once the AJAX request is completed. If you are getting an alert before hand then that indicates that the request completed properly.

Resources