I’m still working on my next ASP.NET MVC article. However, I thought that I would share something in the meantime. I’ve been browsing through Microsoft Visual Studio 2015 Unleashed (3rd Edition) a little bit each day. I borrowed this title from the library, and I was hoping that I would stumble on some little tidbits that I was not yet familiar with.
I’ve been using Visual Studio since the 2008 version, but I’m certain that there are many features I have not tapped into yet. I’ve never taken a dedicated course or read a dedicated book focused solely on Visual Studio, so I can guarantee that there are many gems waiting to be discovered.
The feature that I read about this week that I am most excited about is the Extract Method feature. When working on a legacy code base, have you ever stumbled across a method that does way to many things? I know that I have, and I have to admit that I’ve been guilty of writing some of those methods. Fortunately, Visual Studio makes it easy to break apart methods like that into separate methods that each have single purpose.
In order to leverage the feature, you simply need to highlight a block of code that you would like to move to a separate method. Once you’ve done this, you right-click on the code and select Quick Actions and Refactorings. Next, click on Extract Method. Visual Studio will prompt you to rename the method to something appropriate. Below is a screenshot demonstrating this feature being used for a contrived example.
I’m sure that I’ll find plenty of opportunities to use the Extract Method feature in the coming weeks. In the meantime, I’ll be looking forward to picking up on more hidden gems within Visual Studio. If you have a favorite Visual Studio feature that is not so obvious, feel free to leave a comment below and share your finding.
I deployed a new ASP.NET web application a couple of months back. When I set it up in Visual Studio, I used the individual user accounts option for authentication. The login screen that is included in the generated project includes a “Remember Me” checkbox. I would typically use the application once per day, but I noticed that even though I checked the “Remember Me” checkbox, my credentials would be forgotten the next day.
Heading in, I did not know much of anything about how the various ASP.NET MVC authentication options worked. It turns out the the individual user accounts option results in ASP.NET Identity being used. ASP.NET Identity makes use of OWIN. This option is not the same as Forms Authentication, which is an older technology. I assumed that was what was actually being used, so this was a surprise to me.
After doing some research on my issue, I discovered that the default timeout for ASP.NET Identity with the “Remember Me” option was 14 days. Once I dug deeper, I found that I had a .AspNet.ApplicationCookie cookie with an expiration date that was set 14 days out. However, even with that cookie, my browser would seem to forget the login.
How should “Remember Me” work? If you login without “Remember Me” checked, close your browser, and reopen your browser, it will forget you. If you login with “Remember Me” checked, close your browser, and reopen your browser, it will remember you. The problem for me was occurring if I would leave the browser closed for more than 20 minutes.
During my research, I read that several issues had been fixed in ASP.NET Identity 2.2. I was using 2.1, so I tried upgrading. Unfortunately, that also did not make a difference. At least having the newer version did not result in any additional negative impacts for me.
I did some testing locally, and I noticed that the behavior was different. The browser would still remember me after the 20 minute timeframe had passed. This definitely made me curious regarding whether there was something in the hosting environment that was causing me problems. I did compare the web.config file for both environments and found no relevant differences.
After running through a number of test scenarios, I decided to post a question to Stack Overflow. Someone there mentioned that it may be related to the application pool idle timeout. I am the only person currently using the application, so it is very susceptible to idle timeouts. After consulting with my hosting provider, I found that they do not allow that setting to be modified. Instead, I ended up setting up a scheduled task with the hosting provider to ping the web site and keep the activity going. After that, I was finally seeing the browser maintain my credentials after the 20 minute time period.
Admittedly, this was not the solution that I would have hoped for. If I ever move hosting providers, I will need to setup a similar solution there. However, it was beneficial for me to have the problem, as it afforded me to learn a little bit about how the default ASP.NET MVC authentication setup works. I’m looking forward to digging into it further in the future.
This week I went searching for a classic ASP.NET MVC example that I could use as a basis for making further modifications in future articles. I found an old example Movie Database Application from 2009 that I decided to use. Its features include listing movies from a database, creating new movie entries, and editing existing movie entries. Admittedly, it is not a full-featured application, but it does at least give one a starting point to try to understand how to accomplish some simple goals quickly using ASP.NET MVC.
As I mentioned, the article was written in 2009, so there have been a lot of changes since then. The screenshots were taken using Visual Studio 2008. I decided to refresh the example, using Visual Studio 2015 and ASP.NET MVC 5. Given the release date of the article, it appears that the article was written using ASP.NET MVC 1.0.
The purpose of this article is to share my updated version of this classic example and to discuss a few of the differences that I encountered in recreating it with ASP.NET MVC 5. Although the differences were not extensive, they would be enough to trip up a novice trying to follow along at this point. Granted, there are likely plenty of other newer examples, but I felt like this simple example was a good fit for my planned usage in future articles.
First, you would now use one of the Visual Studio Community versions. 2017 is the newest, but I already had 2015 installed, so I went with that. The feature set of these free versions is quite impressive given the bargain price tag. Given that you would be using a different version of Visual Studio, many of the dialogs are slightly different. However, the differences are quite minor considering that nearly ten years has passed.
Second, creating an Entity Framework (EF) data model has changed slightly, but again the change is very minor. The thing that stood out to me was the fact that EF6 is now trying to come up with appropriate singular names for the objects based upon the plural naming of the database table. In my case, it tried to rename the Movies table to Movy. This update would probably work for a number of different nouns, so it’s understandable why they took that approach.
Third, the generated views now use Razor instead of ASPX. I am of a similar opinion to a lot of developers that Razor is easier to work with, so this is a bonus. Razor became the default view engine with ASP.NET MVC 3, so it makes sense that this would now be the case. Although these views like a fair amount different from those given in the example, they are all generated, so the impact on creating the project is virtually none.
Finally, the biggest difference that I found in this example was that the EF editing code needed to be updated to work with EF6. Some of the methods called in the example, such as ObjectContext.ApplyPropertyChanges, are no longer available. I had to end up replacing the code for adding records to the database and updating records in the database.
Below is the controller method that I used to handle creating a movie record in the database. This includes the updated EF methods necessary to make this work with EF6.
All in all, the process of getting this classic example running with ASP.NET MVC5 in Visual Studio 2015 was accomplished with little difficulties. I consider this to be somewhat impressive, given that the example is nearly ten years old. I will be using this refreshed version of the example in some future articles to demonstrate various features of ASP.NET MVC.