Tag Archives: asp.net mvc

Realtime MVC ModelState errors during debugging

While working on my most recent ASP.NET MVC project, I was having a tough time figuring out why my ModelState was invalid. It was due to the fact that I set the error messages to “Required” or “Invalid” for ALL of my model properties (to support the way the view presents the error messages). In retrospect, this probably wasn’t the smartest choice. Oh well, now I’m stuck figuring out another way to determine the properties causing Model validation to fail.

With Visual Studio 2015, I discovered a marvelous way to get the names of the properties with invalid values… buy using a watch! Watches now support lambda expressions (as long as there are no external dependencies, such as LINQ to SQL), so I thought… this would be a good time to test that functionality.

Since I was interested in the actual name of the property that was causing
ModelState.IsValid to return false, I used the following watch:

ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => x.Key).ToList()

For those who don’t usually use watches, you may not know that a watch can be created directly in the watch window by just typing it in! That is what you will need to do here, since I’m guessing that this value is most likely not in your code…

Untitled

If you get a message under Value saying “This expression causes side effects and will not be evaluated,” just click the refresh button to the right of the message to force-evaluate the watch. Once you do so, you’ll be able to expand the watch object to see your offenders!

Untitled

Pretty neat!

ASP.NET 4.5 MVC Bundling and Minification troubleshooting

ASP.NET 4.5 includes the System.Web.Optimization namespace (it can also be added via NuGet).  This library provides functionality to bundle and minify (B/M) your scripts and stylesheets.

Running some tests today, I noticed that my css image links were breaking whenever I enabled the B/M optimizations. Researching this, I realized I was using relative paths for the images in my CSS files. When B/M occurs, the pseudo path is used to resolve to the B/M’d files, which is usually different from the physical path of the files. In my case, the physical path was at ~/public/css and the B/M pseudo path was ~/bundle/css. Since my images are located in ~/public/images, the relative paths were no longer resolving.

Thinking this would be an easy fix, I changed the B/M pseudo path to mirror the physical path ~/public/css. Unfortunately, this broke my css entirely. Checking the server response, I saw that I was getting a 403.14 error.

Turns out the MVC router blocks calls for B/M files that resolve to actual paths in the project. This is expected behavior, since the router is always called first and is simply doing it’s job (duh)!

The easiest fix for this is to simply change the pseudo path to one level lower than the actual path (in my case, changing it to ~/public/css/bundle). The CSS file will then properly resolve all the relative image paths, regardless of whether or not B/M is on.