Friday, August 17, 2012

MVC 4 Bundles - Where are my css referenced images?


Synopsis:

If StyleBundle or ScriptBundle virtual path is not configured to a valid (virtual) path, css/js files with relative reference links will be broken.

Remedy:

Tip 1:
In BundleConfig.cs and in invoking .Render methods in .cshtml, use the actual (virtual) path:
  • bundles.Add(new StyleBundle("~/Content/themes/main/css").Include("~/Content/themes/main/style.css"));
  • @Styles.Render("~/Content/themes/main/css ")
Tip 2:
Avoid bundling files that aren't located in the same directory

Other things that helped me:
- Be sure to activate Bundle functionality in BundleConfig.cs or Web.config:
  • BundleConfig.cs:  BundleTable.EnableOptimizations = true;
  • Web.config:  
- It is good to bundle even single files;  they'ill at least be minified
- Turn off bundling until deploying to production (it is easier to debug js)

Details:

I'm building my first production app in MVC 4 and exploring the new Bundles functionality.  Bundles is a great feature for optimizing applications for fast downloading.  Once it is configured, it minifies JavaScript and CSS file by doing things such as removing whitespace and even shortening variable names to make it as compact as possible.  It will even bundle (thus the name) css and js files into single files for even greater efficiencies.

When I first bundled my files together, I found I was missing images that were linked relative to my stylesheets.  After some digging I found it isn't a good idea to use a fake virtual path when declaring a StyleBundle for the bundles collection.  Fake virtual paths are ok, as long as the css or js file does not reference a relative file.


Tuesday, April 10, 2012

NuGet not installing NUnit 2.6x tools in Visual Studio .net 2010

I've been working on learning ASP .NET MVC 3 and started a video tutorial from PluralSight - very informative and goes step-by-step to build on learning.

I issue I came across was using the the NuGet utility to add the NUnit package and references to do unit testing. The current version is 2.6x, and the instruction from PluralSight was provided with 2.5x; I found the current version a bit harder to set up than the demo showed. When I used NuGet to get the needed packages for the test project, although it added the needed references, it didn't install the nunit-console (at least I couldn't find it) in the solution directory [$(SolutionDir)\packages\NUnit.2.6.0.12054\tools\nunit-console].

The following has seemed to remedy the issue:
  • Go to the NUnit site and downloaded the .msi.
  • Once installed (by double-clicking the downloaded msi), copy needed files in the install directory [C:\Program Files (x86)\NUnit 2.6\bin]
  • Paste in [$(SolutionDir)\packages\NUnit.2.6.0.12054\tools\]
That did it - the post-build event command line code of the test class library executed without issue. For reference, here is the command line:
$(SolutionDir)\packages\NUnit.2.6.0.12054\tools\nunit-console $(TargetPath)


Friday, October 22, 2010

Bing Maps - Setting the size

I have worked with Bing Maps on a few projects now, but had an issue today that took way too long to figure out. I was trying to set the map display size. According to the documentation I should be able to set a div tag to the dimensions I desire and the map will fill the space when it is loaded. I that but I was getting the default (600x400px). Here's the markup I was using:


<div id="map"></div>

div#map
{
position: relative;
width: 100%;
height: 100%;
}


When I examined the markup and style with firebug, I could see Bing Maps was inserting inline styling which caused my styles to not apply due to specificity.

How was it fixed?
After some digging I found this article:
http://msdn.microsoft.com/en-us/library/bb412551.aspx

Which clued me to the fact that I needed to apply a class style and not a element style. Here is the code that fixed it:


<div id="map" class="mapClass"></div>

div#map.mapClass
{
position: relative;
width: 100%;
height: 100%;
}


Thanks Microsoft!

Tuesday, July 27, 2010

Web User Control: Cannot create an object of type 'System.Int32' from its string representation

I had an interesting bug today. I have been creating web user controls based on the structure of database table fields. When I dropped the web user control on to a web form and tried to access its properties from the codebehind, I was getting no intellisense. I then looked at the designer.aspx.cs and notice the web user control was not registering.

After some digging I found an article that seemed to describe my issue that allowed me to narrow down the problem. I followed the "Simple Tip" of deleting the designer.ascx.cs and then regenerating it and found that it was failing because I was using a public property called "Id". Visual Studio 2008 IDE complains about this, and rightfully so. In hindsight I can see why I shouldn't use a reserved name like "Id" for custom a property.

I changed the property name and all references, and I was back in business. It would have been nice to have a better error message to narrow this down quickly, but I'm grateful to have found the issue quickly.

Monday, June 21, 2010

Classic ASP Login failed for user 'NT AUTHORITY\IUSR'

Most often I build asp .net applications and connect to a local development database using windows authentication. I tried to do the same thing (connect using windows authentication) with a classic asp application I'm working on converting to .net, and got this error:
Login failed for user 'NT AUTHORITY\IUSR'

To remedy the issue I did the following:
1. Open Microsoft SQL Server Management Studio
2. On Registered Servers expand nodes to find database server
3. Right-click on database server > Object Explorer
4. Expand 'Security' node
5. Right-click on 'Logins' > 'New Logins...'
6. Login name: 'NT AUTHORITY\IUSR'
7. Click 'OK'
8. Expand 'Databases' node > 'Your_DB_Name' node > Security
9. Right-click 'Users' > 'New user..."
10. User name: 'Whatever_you_want'
11. Login name: NT AUTHORITY\IUSR
12. In the 'Database role membership', check 'db_datareader' and 'db_datawriter'
13. Click 'OK'

That was it - the classic application was able to connect.

I could have created a new user and connected using SQL Server Authentication mode (I actually tried this first, but then realized the server security settings were set to 'Windows Authentication mode' not 'SQL Server and Windows Authentication mode'), but this allowed me to keep my current server security setting of 'Windows Authentication mode' and keep my connection strings consistent between my classic asp and asp .net applications.

IIS 7 and Classic ASP

Since Classic ASP is over a decade old, Microsoft seems to want to forget it ever existed by making it difficult to deploy classic asp apps. I was reminded of this again in a recent project where I am converting an existing classic asp application to a .net app.

I first wanted to set up the classic asp application on my own (windows 7 64bit) machine so I could dissect it and use some of the existing logic. After adding a new application in IIS and trying to browse to the default page, I was getting a 404 error. After googling a bit, I discovered that there must be some special configuration for classic asp apps... and then I found a link that explained that Classic ASP Not Installed by Default on IIS 7.0. That was nice of them - first issue solved.

Second issue: I was then getting this vague error:
An error occurred on the server when processing the URL. Please contact the system administrator.

Thanks! I am the administrator :(

After some digging, I found out that error messages are not sent to the browser by default for asp pages in IIS7 in case sensitive information is displayed. I followed the instructions and now I can see the errors.

Now I can get to work on the conversion.