Posts

Showing posts with the label ASP.NET MVC

Redirecting Assembly Versions in .NET

Image
As Kyle on South Park would say, "I learned something today." While working on one of our internal applications at work, I was getting the following exception: "System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) File name: 'Microsoft.Practices.ServiceLocation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'" But the funny thing was that all instances of this assembly in the solution were using version 1.3, and not  1.0 as the exception states. In fact, I could find no reference to version 1.0 anywhere in the config files. I enabled assembly bind failure logging (by setting the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1), and was given ...

How I Architect My ASP.NET MVC Apps (Circa Now, 2014)

I recently got a text message from a former co-worker of mine named Mark. Mark was embarking on a new project using ASP.NET MVC and wanted to know how I organize the code in my solutions. I replied with a few text messages giving a quick summary, and told him I'd write a post about it on my blog that would go into more detail. Here's how I architect my ASP.NET MVC solutions. I'm not saying this is the  way to do it, only that it's my  way. A year from now it may be different. Software development is always evolving. Before diving into the different layers of my solutions, there are certain design principles I adhere to that I find very useful and should be mentioned. These are outside the scope of this blog post, but if you aren't familiar with any of them, I highly recommend researching them. They are: SOLID (Single Responsibility Principle, Interface Segregation Principle, Liskov Substitution Principle, Interface Segregation Principle, Dependency Inve...

Funny MVC Routing Quirk

I recently ran into a funny quirk with ASP.NET MVC’s routing. I have a method to retrieve attachments which have been uploaded for orders in an ordering system. Originally, I had the method and the route defined like this: Method signature: public   ActionResult  AttachmentView( int  orderId,  string  filename) Route definition:             routes.MapRoute(name:  "Orders_AttachmentView" ,                 url:  "Orders/AttachmentView/{ orderId}/{filename}" ,                 defaults:  new  { controller =  "Orders" , action =  "AttachmentView"  }             ); When I tried to call this method, I’d get an error from IIS saying that the resource wasn’t ...

Missing or Malformed Service Bindings Can Hose ALL Your Service Calls

I learned something interesting yesterday. After we did a new production deployment at work for one of our websites, we ran into a few issues that hadn't occurred in QA or staging. The first was that, for some reason, calls from the website (which is an ASP.NET MVC 4.5 site) to one of our WCF services, was failing. The service in question is used to retrieve images from Sharepoint. Later, we found another issue, wherein attempts to contact a different service for the purposes of sending email were failing. As it turned out, the two issues, while seemingly unrelated, were caused by the exact same problem. In addition to the endpoints for the two aforementioned services, our Web.config file also contained an endpoint for a third service, that is not yet in use, but is defined and in development. But somehow, the binding for this service didn't make it into the production version of the Web.config file, and therefore when the app tried to make use of any service, an exception wa...

The "TypeError: n.curCSS is not a function‏" Error

Yesterday at work we deployed a new version of one of our websites to production after a successful test cycle in our QA and staging environments. However, once the site was deployed to production, a script error, "TypeError: n.curCSS is not a function‏", would appear whenever an attempt was made to display a jQuery UI dialog. This left me scratching my head. It had worked fine in QA and staging, and the code in question had been around for months. It definitely had something to do with a difference in the production environment, but what? The error in question was occurring in a JavaScript bundle being created by the website (which is an ASP.NET MVC 4.5 site) on start-up. I could view the source and see the offending line. After Googling the error message, I learned that it was related to an incompatibility between versions of jQuery and jQuery UI -- but again, this had worked perfectly in the other environments, and for quite some time. How could it be an incompatibilit...

How to Enable jQuery Client-Side Validation in Dynamically Rendered Partial Views in ASP.NET MVC

Recently I was doing some work in an ASP.NET MVC 4 website and was rendering a partial view to a div element via a jQuery AJAX call. I found that when the partial was included in a view from a controller's action method, all the client-side validation worked correctly, but if I rendered the partial view client-side code that was, for example, just getting the partial view by itself and putting into a div element as I was trying to do, the client-side validation wouldn't work. After doing a little research, I learned that the client-side validation needed to be reset after the partial was rendered via the client-script. To that end, I wrote the following JavaScript function to allow quick and easy resetting of client-side validation in cases such as these. Just pass the CSS selector to the partial's parent form to the function shown below. This, of course, assumes that you also have references to the jQuery unobtrusive validation library. function  ResetValidation(formSe...

An ASP.NET MVC ActionButton HtmlHelper Extension Method

Here's a quick ActionButton HtmlHelper extension method I wrote. Hopefully this will come in handy to someone. For more information on extension methods, check out Microsoft's MSDN document here , or check out one of my earlier posts here. public   static   MvcHtmlString  ActionButton(      this   HtmlHelper  htmlHelper,  string  value,  object  htmlAttributes,  string  actionName,       string  controllerName,  object  routeValues) {      if  ( String .IsNullOrWhiteSpace(actionName))          throw   new   ArgumentException ( "actionName is required." );      if  ( String .IsNullOrWhiteSpace(controllerName))          throw   new   ArgumentException ( "controllerName is required." );   ...

A Very Simple ViewModel Example

Image
I'm currently working on a blog website app in ASP.NET MVC as a pet project. I'm not intending to use this new app instead of Blogger, but rather am doing it as a creative exercise. During the course of this project, I found the need to make use of the MVVM (Model View ViewModel) pattern. In this particular scenario, my implementation of MVVM was very simple, but extremely useful. In this blog post, I'll explain why I needed it, and how it came in handy. The App As I mentioned, my blog app is being written in ASP.NET MVC. I have a BlogPostController class that is used to coordinate the creation, editing, deleting, and retrieving of blog posts. The actual data operations are performed by a repository class that the controller calls. And, of course, the data is displayed and collected via views. The Data Model I'm using a SQL Server database with an Entity Framework data model. Among the tables/entities in my data model are BlogPosts and Tags. Tags are descriptive...

Using a View From a Different Controller in ASP.NET MVC

I'm currently working on a pet project in ASP.NET MVC: an MVC blog engine. I don't plan on using it to host my blog, but rather as an exercise in ASP.NET MVC. In my HomeController class, I want to retrieve the ten most recent blog posts and render them using the Index view for the BlogPost controller. This way, when a user navigates to the default page of the blog, they'll see a list of the most recent entires. But how could I use a BlogPost view (that isn't part of the Shared views folder) from within the HomeController class? To achieve this, I did the following in the Index() method of my HomeController class -- notice the line in bold (this is in MVC 3): public   ActionResult  Index() {     ViewBag.Message =  "My MVC-Powered Blog!" ;     BlogRepository  repo =  new   BlogRepository ();     BlogRepository . BlogPostCriteria  criteria =        ...