locked
Beginners question Why is not attribute HandleError working as the book says RRS feed

  • Question

  • User1366894300 posted

    In the controller class below I have specified HandleError both on the class and on the method so the file in View/Shared/Error.aspx should be called according to this text from a book I'm reading. "When you apply the HandleError filter with no arguments you are specifying that any exception thrown by the methods covered by the filter will result in the
    Views/Shared/Error.aspx view being used. This view simply display the message "Sorry an error occured while processing your request". To test this try to view the details of a product that doesn't exist such as with URL similar to the following (the port number used will differ):
    http://localhost:49506/Product/Details/100I have also specified this in web.config
    <system.web>
        <customErrors mode="On">
        </customErrors>
    as the book says

    The book also says :
    "Here is the HandleError filter applied to the controller class. This means that any exception that is thrown by any of the action methods in the controller will be handled using the custom error policy."

    [HandleError]
        public class ProductController : Controller
        {
             // GET: /Product/Details/5

              [HandleError]
            public ActionResult Details(int id)
            {
                  NorthwindEntities db = new NorthwindEntities();
                  var data = db.Products.Where(e => e.ProductID == id).Select(e => e).Single();
                  return View(data);
            }

    So when I run the url http://localhost:49506/Product/Details/100
    I don't get the message "Sorry an error occured while processing your request" as the book says but instead get an exception in ProductController method
    public ActionResult Details(int id)

    saying

    Sekvensen innehåller inga element
    Beskrivning: Ett undantag som inte kunde hanteras uppstod när den aktuella webbegäran kördes. Mer information om felet och var i koden det uppstod finns i stackspårningen.

    Undantagsinformation: System.InvalidOperationException: Sekvensen innehåller inga element
    Källfel:
    Rad 28:         {
    Rad 29:         NorthwindEntities db = new NorthwindEntities();
    Rad 30:             var data = db.Products.Where(e => e.ProductID == id).Select(e => e).Single();
    Rad 31:                 return View(data);
    Rad 32:         }

    Källfil: C:\Users\tony\programmering\MVC\BasicMVCApplication\Content\Controllers\ProductController.cs    Rad: 30

    So is the book wrong or is it something that I have missed here. I have done exactly what the book has specified.
    I just started to learn MVC

    //Tony

    Friday, April 18, 2014 11:05 AM

Answers

  • User1176121428 posted

    Hi Tojo

    Thanks for your post.

    You don't need to decorate your controller classes with the [HandleError] attribute. It works without that as well for the default Error view. You may provide that attribute if you

    want a specific custom view for your action or controller. Like so:

    [HandleError(View = "CustomError")]

    Hope this helps you.

    Best Regards,

    Eileen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 21, 2014 2:53 AM

All replies