Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to display exception messages to users

$
0
0

In my web app I show messages to the users by using ViewBag. In the controller method I write a string that I can set a message on:

public IActionResult Index(string message = null)
{
    if(!String.IsNullOrEmpty(message))
    {
        ViewBag.Message = message;
    }
    return View();
}

I feel this is working fine for me(please say if you know better ways). But sometimes I want to show the normal error page with an exception message. 

I see that the normal app template comes with an error page. But I don't understand how it works. I'm sure it's just my lack of c# knowledge at work. But the error page viewmodel property is set by a class called Activity in the System.Diagnostics namespace. What I don't understand is 1) how does info get passed to this? 2) what is the property static Activity Current? I didn't think you could make a property with the same type as the class like that? 

Maybe I should just do it like I didn in the controller with the viewbag, only for a new errorpage where I can display more than one message? It would be easiest and 100% work. I just want to know how other people do it. Thanks.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>