ASP.net MVC: Exception e chiamata AJAX

felino

Utente Attivo
12 Dic 2013
940
10
18
Aci Catena (Catania)
Ciao a tutti,
sto realizzando un'applicazione basata su ASP.net MVC.

Tramite Ajax effettuo la chiamata ad diversi metodo, esempio può essere:
Codice:
public ActionResult MyMethod(DateTime ref_date) { 
            try
            {
                //code

                return new FileContentResult(stream.ToArray(), "application/pdf");
            }
            catch (Exception ex)
            {
                return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
            }
}

Se l'eccezione non scatta, nella success della chiamata ajax eseguo la procedura desiderata...

Se viene generata l'eccezione, scatta l'error della chiamata ajax, ma non riesco a catturare in alcun modo l'ex.Message:
Codice:
error: function (err, type, httpStatus) {
    console.log(err);
    var failureMessage = 'Error occurred in ajax call ' + err.status + " - " + err.responseText + " - " + httpStatus;
    console.log(failureMessage);
    console.log(err.responseText);
}

Se ad esempio il metodo va in eccezione perchè il file usato come modello è lockato da un altro processo, nell'eccezione del metodo lo vedo, nella chiamata Ajax no, non riesco a catturarlo.

Probabilmente sbaglio il tipo di ritorno in caso di exception?

Grazie.
 

felino

Utente Attivo
12 Dic 2013
940
10
18
Aci Catena (Catania)
Ho trovato l'errore, modificando questa parte della chiamata ajax:

- se la risposta è success allora il type atteso è un blob
- se la risposta è error allora il type atteso è un text

Codice:
xhr: function () {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 2) {
                    if (xhr.status == 200) {
                        xhr.responseType = "blob";
                    } else {
                        xhr.responseType = "text";
                    }
                }
            };
            return xhr;
        },

Grazie! ;)
 
Discussioni simili
Autore Titolo Forum Risposte Data
A [ASP.Net] Gestire il profilo ADMIN in un progetto ASP NET e pattern MVC ASP.NET 0
S Asp.net MVC Hosting - SQL Server Programmazione 0
R Sviluppatore ASP.NET Offerte e Richieste di Lavoro e/o Collaborazione 0
P Passagio dati complessi tra una ASP.NET webapi e Angular ASP.NET 1
P [ASP.Net] Problema ERR_INCOMPLETE_CHUNKED_ENCODING 206 (Partial Content) con Font ASP.NET 4
A Mantenere la validazione reindirizzandoci su un secondo sito WEb. (ASP.NET) ASP.NET 0
StephenSoftware [ASP.Net] Pagina Master ed eventi... ASP.NET 0
NuviaSoftware [Retribuito] [Cerco] Programmatore ambienti ASP.NET,. Net Azure web apps, Javascript Offerte e Richieste di Lavoro e/o Collaborazione 0
V [ASP.Net] avento keydown crh13 enter ASP.NET 0
A [ASP.Net] Dropdownlist ASP.NET 0
S [ASP.Net] [ASP] Upload Image ASP.NET 6
riminese77 [ASP.Net] Popup Bootstrap in datagrid ASP.NET 0
K [ASP.Net] Realizzazione menu a tendina dipendenti ASP.NET 1
E [ASP.Net] Access 2016 ? Non si usa Microsoft Jet 4.0 ma Engine 2016 o 2010. Non va ! ASP.NET 2
M [ASP.Net] [ASP] geoblocking e web.config ASP.NET 3
L [ASP.Net] Realizzare modulo contatti PopUp ! ASP.NET 1
K [ASP.Net] Problema stampa e modifica ms sql ASP.NET 0
D [ASP.Net] Non elencare nella select i duplicati dallo split Classic ASP 9
K [ASP.Net] Utilizzare e modificare dati linq ASP.NET 0
felino [ASP.Net] [VbScript] Catturare valore del Hard Faults (Page Fault) ASP.NET 0
andreacata31 [ASP.Net] Passare da Windows Application a sito Web ASP.NET 1
stellare21 [ASP.Net] Redirect a una pagina ASP.NET 2
M [ASP.Net] Cosa MI sfugge? ASP.NET 1
Etinetsrl [Retribuito] - Sviluppatore ASP.net / PHP per piattaforma CMS proprietaria Presentati al Forum 0
E [ASP.Net] Autenticazione integrata windows ASP.NET 0
momeraths [ASP.Net] Consiglio su Visual Studio ASP.NET 3
B Porting da Classic ASP ad ASP.NET Classic ASP 1
onweb (offro) aplicazioni web (asp.net) Offerte e Richieste di Lavoro e/o Collaborazione 0
V Asp.net Manuali Video ASP.NET 2
G Errore quando Sto ospitando i miei siti ASP.NET ASP.NET 1
M Trasferire un sito Asp.net da un server ad un altro ASP.NET 0
felino [ASP.NET] Concatenare una ViewBag al testo ASP.NET 1
G cerco progarmmatore asp net su caserta Offerte e Richieste di Lavoro e/o Collaborazione 0
M ASP.NET Response.Redirect che si apre in una nuova finestra ASP.NET 1
M Session timeout in ASP.NET ASP.NET 1
M Lanciare una funzione asp.net con javascript. E' possibile? ASP.NET 0
M Disabilitare cache browser sito asp.net ASP.NET 1
onweb Vendo applicazione asp.net - block notes Altri Annunci 0
maxbossi Regolamento Regolamento del forum ASP.Net - Leggere prima di postare!!! ASP.NET 0
S VS2010: Variabili Session in ASP net ASP.NET 2
S PHP vs ASP.NET PHP 0
F Perché usate ASP.net? ASP.NET 21
R Asp.net & Sql Server ASP.NET 3
X Differenze web form asp.net e form html ASP.NET 2
B ASP.Net, C#, VB.Net... sono confuso... ASP.NET 1
S How to build ASP.NET Media Player Control in C# .Net code .NET Framework 0
S How to build ASP.NET Media Player Control in C# .Net code ASP.NET 0
L Cerco Programmatore Web Asp.net Offerte e Richieste di Lavoro e/o Collaborazione 1
voldemort rs.AddNew in ASP.Net? ASP.NET 1
voldemort Script calendario eventi ASP.Net ASP.NET 1

Discussioni simili