Exceção sem tratamento o que pode ser esse erro?

16/06/2019

0

Criei um login com forms autenticathion e ele esta funcionando quando eu logo e entro no create, delete, ....ele aparece o erro Exceção sem tratamento e estou iniciando com asp. C#.


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using estoque.Models;

namespace estoque.Controllers
{
public class GenerosController : Controller
{
private LivroContexto db = new LivroContexto();

// GET: Generos
[Authorize]
public ActionResult Index()
{
return View(db.Generos.ToList());

}


// GET: Generos/Details/5
[Authorize]
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Genero genero = db.Generos.Find(id);
if (genero == null)
{
return HttpNotFound();
}
return View(genero);
}

// GET: Generos/Create
[Authorize]
public ActionResult Create()
{
return View();
}

// POST: Generos/Create
// Para se proteger de mais ataques, ative as propriedades específicas a que você quer se conectar. Para
// obter mais detalhes, consulte https://go.microsoft.com/fwlink/?LinkId=317598.
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Nome")] Genero genero)
{
if (ModelState.IsValid)
{
db.Generos.Add(genero);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(genero);
}

// GET: Generos/Edit/5
[Authorize]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Genero genero = db.Generos.Find(id);
if (genero == null)
{
return HttpNotFound();
}
return View(genero);
}

// POST: Generos/Edit/5
// Para se proteger de mais ataques, ative as propriedades específicas a que você quer se conectar. Para
// obter mais detalhes, consulte https://go.microsoft.com/fwlink/?LinkId=317598.
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Nome")] Genero genero)
{
if (ModelState.IsValid)
{
db.Entry(genero).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(genero);
}

// GET: Generos/Delete/5
[Authorize]
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Genero genero = db.Generos.Find(id);
if (genero == null)
{
return HttpNotFound();
}
return View(genero);
}

// POST: Generos/Delete/5
[Authorize]
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Genero genero = db.Generos.Find(id);
db.Generos.Remove(genero);
db.SaveChanges();
return RedirectToAction("Index");
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Diego Ribeiro

Diego Ribeiro

Responder

Posts

17/06/2019

Alex William

Olá amigo, tudo bem?

Para você captar a "Exception" que ta sendo lançada, use Try/Catch nas suas funções:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using estoque.Models;

namespace estoque.Controllers {
    public class GenerosController : Controller {
        private LivroContexto db = new LivroContexto();

        // GET: Generos
        [Authorize]
        public ActionResult Index() {
            try{
                return View(db.Generos.ToList());
            }catch(Exception e){
                throw e;
            }
        }

        // GET: Generos/Details/5
        [Authorize]
        public ActionResult Details(int? id) {
            try{
                if (id == null) {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Genero genero = db.Generos.Find(id);
                if (genero == null) {
                    return HttpNotFound();
                }
                return View(genero);
            }catch(Exception e){
                throw e;
            }
        }

        // GET: Generos/Create
        [Authorize]
        public ActionResult Create() {
            try{
                return View();
            }catch(Exception e){
                throw e;
            }
        }

        // POST: Generos/Create
        // Para se proteger de mais ataques, ative as propriedades específicas a que você quer se conectar. Para
        // obter mais detalhes, consulte https://go.microsoft.com/fwlink/?LinkId=317598.
        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Nome")] Genero genero) {
            try{
                if (ModelState.IsValid) {
                    db.Generos.Add(genero);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                return View(genero);
            }catch(Exception e){
                throw e;
            }
        }

        // GET: Generos/Edit/5
        [Authorize]
        public ActionResult Edit(int? id) {
            try{
                if (id == null) {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Genero genero = db.Generos.Find(id);
                if (genero == null) {
                    return HttpNotFound();
                }
                return View(genero);
            }catch(Exception e){
                throw e;
            }
        }

        // POST: Generos/Edit/5
        // Para se proteger de mais ataques, ative as propriedades específicas a que você quer se conectar. Para
        // obter mais detalhes, consulte https://go.microsoft.com/fwlink/?LinkId=317598.
        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Nome")] Genero genero) {
            try{
                if (ModelState.IsValid) {
                    db.Entry(genero).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(genero);
            }catch(Exception e){
                throw e;
            }
        }

        // GET: Generos/Delete/5
        [Authorize]
        public ActionResult Delete(int? id) {
            try{
                if (id == null) {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                Genero genero = db.Generos.Find(id);
                if (genero == null) {
                    return HttpNotFound();
                }
                return View(genero);
            }catch(Exception e){
                throw e;
            }
        }

        // POST: Generos/Delete/5
        [Authorize]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id){
            try{
                Genero genero = db.Generos.Find(id);
                db.Generos.Remove(genero);
                db.SaveChanges();
                return RedirectToAction("Index");
            }catch(Exception e){
                throw e;
            }
        }

        protected override void Dispose(bool disposing) {
            try{
                if (disposing) {
                    db.Dispose();
                }
                base.Dispose(disposing);
            }catch(Exception e){
                throw e;
            }
        }
    }
} 


Para ficar mais especifico, voce ainda pode colocar um "BreakPoint" nos "throw e;" e ai voce consegue ver qual função ta dando erro, e se voce olhar a "StackTrace" da exceção você pode ver qual a linha ta dando problema.

Espero ter ajudado. :D

P.S.: Demorei mais tempo identando seu codigo, do que colocando Try/Catch porque você não usou as tags de code. Por favor, use nos proximos posts. Obrigado.
https://www.devmedia.com.br/forum/todo-mundo-deveria-ler-isso-antes-de-postar/602881
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar