In an earlier tip I disagreed with one of Microsoft's recommendations for handling exceptions. I figure I'm on a roll, so here's another objection to some Microsoft advice on handling errors.
More
Posted by Peter Vogel on 06/03/20190 comments
I hate creating two or more Views that share a lot of HTML because it doubles my maintenance burden by forcing me to keep the two Views in sync. The usual reason I'm doing this is because I have single page that's supporting two user roles and I need to suppress or include some HTML based on the current user's authorization level.
More
Posted by Peter Vogel on 05/31/20190 comments
I have to admit that I've never really understood regular expressions. But if there was any reason that I was going to learn to use them, it would be when doing searches in Visual Studio.
For one thing, regular expressions are easy to invoke when doing a search: When you press Ctrl_F to get the Find dialog box, all you have to do is click the asterisk (*) to the left of the of scope dropdown list to start using regular expressions in your search.
More
Posted by Peter Vogel on 05/30/20190 comments
You want to put together an error message that includes information from the Customer object that caused the problem. Since you're not repeatedly modifying a single string variable, using StringBuilder would be overkill. You still have three options.
Your first option is the most obvious: Concatenation. Here's some code that assembles a message from constant string and some values:
More
Posted by Peter Vogel on 05/29/20190 comments
When I pressed F5 to start debugging and Visual Studio found a compile-time error, nothing irritated me more than the dialog box Visual Studio popped up that asked, "There were build errors. Would you like to continue and run the last successful build?"
Let me be clear: No, I didn't want to run "the last successful build." I never wanted to run "the last successful build." Who in the world would want to run "the last successful build?" Like any other rational human being in the world, I wanted to run the version of the code with the changes I had just finished making ... well, after I fixed the compile errors, I mean.
More
Posted by Peter Vogel on 05/08/20190 comments
Microsoft has some "best practice" advice to share on how to handle exceptions (a topic I've discussed elsewhere). The Microsoft article is well worth reading ... but there's one piece of advice that I disagree with (talk about hubris, eh?).
More
Posted by Peter Vogel on 04/26/20190 comments
In an earlier post, I discussed the three objects that Microsoft has provided for calling Web Services: HttpWebRequest, WebClient and HttpClient. At the time, I suggested WebClient was the simplest solution, unless you wanted to take advantage of HttpClient's asynchronous processing.
More
Posted by Peter Vogel on 04/25/20190 comments
The biggest change in handling Views in ASP.NET Core is that you can now have more than one _Layout.cshtml file in your project and your Views will pick the one "closest to them." In ASP.NET MVC, you set a View's Layout property to the full path name of the Layout file to be used, like this:
More
Posted by Peter Vogel on 04/04/20190 comments
In an earlier column, I showed how to access configuration settings in your project's appsettings.json file and then make those settings available throughout your application as IOptions objects.
More
Posted by Peter Vogel on 03/26/20190 comments
In many of these tips, I've suggested ways that you might want to change Visual Studio's default configuration. That's not always a good thing. For example, I've known some developers who, because of some problem, had to re-install Visual Studio and lost all their customizations. I sometimes find myself at a client's site, working on a computer that isn't mine and looking foolish because some customization I depend on is gone ... or I used to, at any rate.
More
Posted by Peter Vogel on 03/21/20190 comments
By default, if you add a Razor Page to your project's Pages folder, the URL that you use to access that page is based on the Page's file name. So, for example, a Page with the file name CustomerManagement.cshtml is retrieved with the URL http://<server name>/CustomerManagement. There is an exception to this: A Page with the file name Index.cshtml is retrieved with just http://<server name>. This convention extends to subfolders: If that CustomerManagement.cshtml file is in the Pages/Customers folder then its URL is http://<server name>/Customers/CustomerManagement (and Index.cshtml in the same folder has http://<server name>/Customers as its URL).
More
Posted by Peter Vogel on 03/15/20190 comments
I never get my code right the first time. And, even after my code passes all its tests, it's still not right. That's because I will have learned a lot about the problem when writing my code (wouldn't it be awful if that didn't happen?). But, unfortunately, much of my code reflects decisions made in an early, more ignorant stage of this learning process. As a result, I typically want to take some time, after the code passes its tests, to rewrite my code and make it "better."
More
Posted by Peter Vogel on 03/13/20190 comments