Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.

Programming and Learning from SD MVC ASP.Net MVC
Avoid ClickJacking in ASP.Net Core 1.0
Please do me a favor and read this on my Gooroo site. (if I get enough hits, I get a payment) Thanks! I recently received the status report of a penetration test of my ASP.Net Core 1.0 MVC site done by our IT team. They used Rapid 7s vulnerability/penetration testing tool called Nexpose. I wish I had known about it before I thought I was “done” (I haven’t written code for it for a few weeks, it has been tested and is ready to deploy), but that’s a different story. I should be doing this testing as ......

Posted On Tuesday, July 26, 2016 2:41 PM

Pre-Compiling your MVC Views
One part of MVC Views that can cause problems is that the cshtml can cause run-time errors. This can lead to bugs in Production or found by QA that could be avoided if it was built when you build the project. Pre-compiling can be turned on to avoid those problems. You add this in the project file of your web application, by setting the <MvCBuildViews>true&l... in the .csproj. See for more details. Another reason to pre-compile ......

Posted On Tuesday, May 28, 2013 12:28 PM

Knockout Js and External MVC cshtml templates
I can pass the name of the template to the controller like this (/Templates/KnockoutTemplat... where 'radial' is the name of a view (radial.cshtml), return a partial view of that name and have Knockout put it in the template block. My Controller: public class TemplatesController : Controller{ public TemplatesViewModel viewModel { get; set; } public ActionResult KnockoutTemplate(string templateName, int? id) { this.viewModel.Id = id; return PartialView(templateName.Re... ......

Posted On Friday, August 17, 2012 8:18 AM

Scripts not loading with ScriptBundles?
We upgraded a javascript library and pointed to the *.min.js files in our MVC4 application. They weren't getting downloaded and were not in the source code when running in debug="true", but worked in debug="false".

The lesson is to remove .min from your js files or the ScriptBundle doesn't return them for downloading.
bundles.Add(new ScriptBundle("~/bundles/x").Include(

Posted On Thursday, August 16, 2012 2:30 PM

Better single-page apps with ASP.NET MVC 6 - Steve Sanderson NDC2016
I watched the very interesting talk by Steve Sanderson (KnockoutJs creator) from NDC 2016 yesterday and took some notes and screenshots that are worth sharing. Watch the video, but here is my summary. With Steve on the MVC team, there are a lot of helpful and timesaving features coming to MVC. He says MVC 6, but from the middleware and Mac he uses it has to be ASP .Net Core 1. I’m interested in the NodeServices, but I’m currently using Knockout and want to use Aurelia so it’d be good to see more ......

Posted On Friday, March 4, 2016 5:31 PM

Updating New MVC Project NuGet packages could not load Microsoft.Owin.Security
File new project, MVC (Visual Studio 2013 in April 2014) Update all NuGet packages and I get this: Server Error in '/' Application. Could not load file or assembly 'Microsoft.Owin.Security, Version=, Culture=neutral, PublicKeyToken=31bf3856ad36... or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)Description: An unhandled exception occurred during the execution of the current web request. Please ......

Posted On Sunday, April 12, 2015 2:01 AM

Quick Guide to setup a MVC project for Unit Testing
Reposted on my new blog. Come visit me there!A barrier to getting into writing executable tests (Unit Tests, integration tests, automated UI tests, etc) with some people I work with, is not knowing how to get started. I believe the desire and the recognition of the value and importance of this testing is growing, but I want to help people get over that hump. Read the MSDN Unit Testing MVC article. consider splitting controllers into a different project as suggested Don’t put your data access code ......

Posted On Thursday, September 25, 2014 9:18 PM

MVC OnActionExecuting to Redirect
I recently had the following requirements in an MVC application: Given a new user that still has the default password When they first login Then the user must change their password and optionally provide contact information I found that I can override the OnActionExecuting method in a BaseController class.public class BaseController : Controller { [Inject] public ISessionManager SessionManager { get; set; } protected override void OnActionExecuting(ActionExe... filterContext) { // call ......

Posted On Tuesday, August 12, 2014 3:59 PM

Steps to Get MVC running with Ninject
enable package restore on solution Nuget Ninject.MVC3 (pre-release available?) * adds Ninject, Ninject.Web.Common, Ninject.Web.Mvc * App_Start/NinjectWebCommon.cs -> has WebActivator.PreApplication... attribute -> runs at startup add interface and class for testing (IBikeManager and BikeManager) add controller constructor to HomeController that takes in the interface - run website => Error "Error activating IBikeManager No matching bindings are available, and the type is not self-bindable. ......

Posted On Wednesday, November 20, 2013 10:46 AM

When to set ws2007FederationHttpBinding establishSecurityContext to false
Turning the establishSecurityContext off (it’s on by default) was recommended in the IAC course on Pluralsight by Dominick Baier in the Security Best Practices module, State Management when making lots of short calls. “Other protocols like the WS 2007 Federation HttpBinding, they support state and unfortunately it’s turned on by default…. [uses] WS-Secure Conversation which is kind of heavy handed. It is quite complex as well and it has some performance implications.” ~ Pluralsight video ......

Posted On Wednesday, June 12, 2013 11:26 AM

Faking the WebApi User
I needed to unit test a WebAPI call in my MVC 4 application that checks the user's role. I'm doing this in my MVC controllers with the following code using FakeItEasy (I should do a post on that sometime): this.UserPrincipalFake = A.Fake<IPrincipal>(); A.CallTo(() => this.UserPrincipalFake.Iden... A.CallTo(() => this.UserPrincipalFake.Iden... this.HttpContextBaseFake = A.Fake<HttpContextBase&g... this.HttpContextBaseFake.User ......

Posted On Wednesday, June 5, 2013 10:29 AM

MVC Performance Tip
MVC can work with .cshtml or .aspx pages (you can mix Web Forms and Razor views in the One ASP.Net that Scott Hanselman talks about). This means MVC will search for a view with endings of .aspx, .ascx, .cshtml, .vbcshtml, in the Controller directory (if you have a HomeController, then it it would look in Views/Home, then Views/Shared). If you aren’t using aspx pages it’s doing extra work it doesn’t need to do. See Dave Ward’s post on the importance of using debug=false and Marcin Doboz’s post on ......

Posted On Friday, May 31, 2013 8:10 AM

Uncaught SyntaxError: Unexpected token u. file JavaScript Error in MVC
I was getting a JavaScript error that pointed to line number 1 of my MVC page. I have a form on the page and expected the Unobtrusive Validation to work with the Data Annotations. It took me a while to realize the validation messages weren't showing. After I while I found that I was missing the @Html.ValidationMessageFor(m => m.Name). Adding that for each field fixed it. My password reset with token example. I was missing line 12 and 17. 1: @using (Html.BeginForm("PasswordRe... "Home")) 2: { ......

Posted On Monday, May 20, 2013 1:45 PM

MVC Validation of a Password with a Regular Expression
Re-posted on January 19th, 2018 to have requirement (specification) that the password has to be at least 7 characters long and contain a special character (~@#$&*()-_+=) which are all the specials in the number keyboard row. I found that using the RegularExpression DataAnnotations is really slick, but there was a point that caught me for a while. The problem is that @".*([!@#$&*()-_+=]+).*$" works in the UI, but fails in the unit test. @"[!@#$&*()-_+=]+" works in the ......

Posted On Friday, April 26, 2013 11:16 AM

Visual Studio 2012.2 Hanging When Editing Razor Pages
Since I installed the v2 update, Visual Studio has been hanging on occasion. The CPU for the process jumps to 25% and stays there and Visual Studio stops responding. I found a Microsoft Connect feedback which describes the same problem. It seems to be connected to intellisense for Knockout. There is a registry hack to turn it off, and this has worked for me. On March 13th, they report that the fix will be in VS Update 2, which is currently in CTP4 state (on March 19, 2013) with a “go-live” license.Update ......

Posted On Tuesday, March 19, 2013 8:06 AM

WebAPI Dependency Injection
If you try to dependency injection into a constructor with WebAPI, you’ll get an exception that says you need a parameter less constructor. The same is true for MVC controllers, but that is a different topic. Microsoft contains a good guide on how to get this working. There is also a useful StructureMap.DependencyReso... nuget package by statish860 that does it for you. Get the nuget package, then add GlobalConfiguration.Configu... = new DependencyResolver(ObjectFa... ......

Posted On Friday, December 14, 2012 10:29 AM

WebAPI and MVC4 and OData
I was looking closer into WebAPI, specificially how to use OData to avoid writing GetCustomerByCustomerId(int id) methods all over the place. I had problems just returning IQueryable<T> as some sites suggested in the WebpAPI (Assembly System.Web.Http.dll, v4.0.0.0). I think things changed in the release version and the blog posts are still out of date. There is no [Queraable] as the answer to this question suggests. Once I get WebAPI.Odata Nuget package, and added the [Queryable] to the method ......

Posted On Wednesday, September 19, 2012 9:08 AM

MVC 4 Authentication
Edit on April 10, 2014: A lot has changed since I started this now out of date post and Microsoft has created and updated the authentication system to ASP.Net Identity. Please look at this first. Here's a video on the subject.First: After searching for awhile to figure out what’s new/different with MVC 4 and forms authentication, this is the best article I've found on the subject: ......

Posted On Thursday, August 30, 2012 1:20 PM

Specification Test for the Authorized Attribute in MVC
I wanted to Unit Test (in BDD I’d call it a specification) that the controller had the authorize attribute so I found this approach in a GoodSearch a while back and forgotten who to give credit for it, but I thought I’d post it, so I won’t have to search for it next time. I put this in a base class and it’s been very useful. EDIT: March 21st, 2013 I added a way to also verify the correct roles are in the attribute. This is especially nice, sine the attribute takes strings. [Authorize(Roles = "Super ......

Posted On Friday, March 15, 2013 1:27 PM

MVC Forms Styling from Twitter Bootstrap
I was looking at Twitter’s Bootstrap awhile back and found that their CSS, plus some validation CSS from the MVC template, and approach to forms will work nicely for my MVC project. I didn’t want to use all of the Bootstrap CSS, so I took out only what was needed. /* Surround the * after the form label on required fields*/ .requiredFormLabelDiv { display: block; } .requiredFormStar { color: red; margin-left: 10px; } .message-info { border: 1px solid; clear: both; padding: 10px 20px; } .message-error ......

Posted On Friday, January 25, 2013 1:24 PM

Using Json.Net JObject with WebApi
I needed to pass some data from JavaScript to my WebAi Controller and came across Rick Strahl's passing multiple POST parameters to Web API article, his use of the JObject from Json.Net and dynamics works really well. Note usage of the jsondata[“UserId”].Value<... This is a simplified example of the code I‘m using.Note: you can also return the JObject. System.Web.Mvc.HttpPost] public HttpResponseMessage UpdateUserInfo(JObject jsonData) { // ......

Posted On Thursday, January 17, 2013 3:21 PM

MVC 4 string.cshtml override
In order to get placeholder or maxLength or other Html attributes to work with the Html.EditorFor methods, you need to create an override file in Views\Shared\EditorTemplate... @{ // make placeholder work // // from [Display(Prompt = IDictionary<string, object> attributes = new Dictionary<string, object>(); if (!string.IsNullOrEmpty(View... { attributes.Add("placeholder", ......

Posted On Wednesday, December 19, 2012 9:41 AM

Localization of Database Strings in .Net
I have several database tables that have a description column that I need to display in the UI. .Net has .resx files that will help with the translation of the strings, when the Thread.CurrentCulture.UICul... is set, but I needed a custom approach for the strings that are stored in the database and not in the .resx files. Note: this will only work for static tables that are added to at development time, because each addition will require an addition to the resx file for each language and a re-deployment ......

Posted On Monday, November 26, 2012 10:33 AM

Custom Error Handling in MVC
I found this article and it told me everything I needed to know:

Posted On Thursday, November 15, 2012 2:37 PM

Copyright © Aligned | Powered by: