<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Martin Normark&#039;s blog &#187; ASP.NET MVC</title>
	<atom:link href="http://martinnormark.com/tag/asp-net-mvc/feed" rel="self" type="application/rss+xml" />
	<link>http://martinnormark.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 21:06:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Unit Testing ASP.NET MVC Controller Action contains specific ActionFilter Attributes</title>
		<link>http://martinnormark.com/unit-testing-asp-net-mvc-controller-action-contains-actionfilter?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/unit-testing-asp-net-mvc-controller-action-contains-actionfilter#comments</comments>
		<pubDate>Tue, 15 May 2012 21:06:09 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Unit testing]]></category>
		<category><![CDATA[ActionFilters]]></category>

		<guid isPermaLink="false">http://martinnormark.com/?p=424</guid>
		<description><![CDATA[ActionFilters in ASP.NET MVC are great. You can now easily share logic between controllers without having to inherit from a base controller, that does the common work. I have a content heavy application that supports a set of layouts. Each layout &#8230; <p><a class="btn small" href="http://martinnormark.com/unit-testing-asp-net-mvc-controller-action-contains-actionfilter">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/dd410209.aspx">ActionFilters</a> in ASP.NET MVC are great.</p>
<p>You can now easily share logic between controllers without having to inherit from a base controller, that does the common work.</p>
<p>I have a content heavy application that supports a set of layouts. Each layout is rendered by setting the Layout view dynamically, which I do from an ActionFilter. My ActionFilter is fully tested, but when you delegate work to an ActionFilter, you should write a test that ensures the filter is defined on the action.</p>
<p>Not surprisingly, this is very simple and relies on reflection (Download from Gist: <a href="https://gist.github.com/2605628">https://gist.github.com/2605628</a>):</p>
<pre class="prettyprint">/// &lt;summary&gt;
/// Verifies the controller action, contains an attribute of the specified attributeType.
/// &lt;/summary&gt;
/// &lt;param name="controller"&gt;The controller.&lt;/param&gt;
/// &lt;param name="action"&gt;The action method.&lt;/param&gt;
/// &lt;param name="attributeType"&gt;Type of the attribute to look for.&lt;/param&gt;
/// &lt;returns&gt;Returns true if the attribute was present on the action. Otherwise false.&lt;/returns&gt;
public static bool VerifyControllerActionAttribute(this Controller controller, Func&lt;ActionResult&gt; action, Type attributeType)
{
    MethodInfo methodInfo = action.Method;
    object[] attributes = methodInfo.GetCustomAttributes(attributeType, true);
    return attributes.Any(a =&gt; a.GetType() == attributeType);
}</pre>
<p>And the usage looks like this:</p>
<pre class="prettyprint">[TestMethod]
public void Index_ContainsTemplateLayoutAttribute()
{
    HomeController controller = new HomeController();
    bool containsAttribute = controller.VerifyControllerActionAttribute(controller.Index, typeof(TemplateLayoutAttribute));
    Assert.IsTrue(containsAttribute);
}</pre>
<p>Need an introduction? Watch a video about <a href="http://vimeo.com/32617893">unit testing ASP.NET and MVC based web apps</a> on Vimeo, published by Typemock.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/unit-testing-asp-net-mvc-controller-action-contains-actionfilter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>ASP.NET MVC Localize Numeric (data-val-number) Validation</title>
		<link>http://martinnormark.com/asp-net-mvc-localize-numeric-data-val-number-validation?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/asp-net-mvc-localize-numeric-data-val-number-validation#comments</comments>
		<pubDate>Sat, 05 May 2012 11:08:31 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://martinnormark.com/?p=349</guid>
		<description><![CDATA[Another buggy experience in ASP.NET MVC. This has to do with validation, more specifically numeric validation. Whenever you have a numeric property on your view models, ASP.NET MVC automatically adds some implicit validation to ensure that whatever entered in fact is a numeric value. &#8230; <p><a class="btn small" href="http://martinnormark.com/asp-net-mvc-localize-numeric-data-val-number-validation">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>Another buggy experience in ASP.NET MVC. This has to do with validation, more specifically numeric validation. Whenever you have a numeric property on your view models, ASP.NET MVC automatically adds some implicit validation to ensure that whatever entered in fact is a numeric value.</p>
<p>Might be good in most cases, but here's the problem. The validation mechanism writes the error messages in english. So for any app that targets a non-english audience, this needs to be changes.</p>
<p>But that's easier said than done. The code in ASP.NET MVC that generates the error message for the implicit numeric validator, looks like this:</p>
<pre class="prettyprint">private static string MakeErrorString(string displayName) {
  // use CurrentCulture since this message is intended for the site visitor
  return String.Format(CultureInfo.CurrentCulture, MvcResources.ClientDataTypeModelValidatorProvider_FieldMustBeNumeric, displayName);
}</pre>
<p>To make matter worse, this is a method from an internal, sealed class. They didn't want you to change this.</p>
<p>The only option is to remove the validator, and get rid of the implicit validation. To be honest, I prefer to take care of validation myself, through data annotations on my view models. I don't like when too much gets done implicitly. I want to be in control, and I want to know what is going on.</p>
<p>You can remove the validator in Global.asax:</p>
<pre class="prettyprint">foreach (ModelValidatorProvider prov in ModelValidatorProviders.Providers) {
  if (prov.GetType().Equals(typeof(ClientDataTypeModelValidatorProvider))) {
    ModelValidatorProviders.Providers.Remove(prov);
    break;
  }
}</pre>
<p>If you'd like to keep the numeric validation, but be able to override the error message, there's a great way to <a href="http://jwwishart.blogspot.com/2011/03/custom-server-and-client-side-required.html">roll your own ClientDataTypeModelValidatorProvider</a> that does this the right way.</p>
<h2>Things will get better</h2>
<p>I was browsing through the source code of ASP.NET MVC 4, and found <a href="http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/06f52b894414#src%2fSystem.Web.Mvc%2fClientDataTypeModelValidatorProvider.cs">a changeset</a> that looks like will fix this issue. In short, it will be possible to define your own <a href="http://msdn.microsoft.com/en-us/library/system.web.http.validation.providers.clientdatatypemodelvalidatorprovider.resourceclasskey(v=vs.108).aspx">ResourceClassKey</a>, and the MVC framework will use that before using default error messages. Nice!</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/asp-net-mvc-localize-numeric-data-val-number-validation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>ASP.NET MVC 3 Windows Authentication problem &#8211; redirects to Account/Login</title>
		<link>http://martinnormark.com/asp-net-mvc-3-windows-authentication-problem-redirects-to-account-login?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/asp-net-mvc-3-windows-authentication-problem-redirects-to-account-login#comments</comments>
		<pubDate>Sun, 06 Nov 2011 15:45:19 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[quirks]]></category>

		<guid isPermaLink="false">http://martinnormark.com/?p=342</guid>
		<description><![CDATA[ASP.NET MVC 3 has its quirkiness here and there. Sometimes it's a lack of support for something, and other times it's downright buggy. An issue I came across recently, is something to do with Windows Authentication. There's a lot of resource on &#8230; <p><a class="btn small" href="http://martinnormark.com/asp-net-mvc-3-windows-authentication-problem-redirects-to-account-login">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>ASP.NET MVC 3 has its quirkiness here and there. Sometimes it's a lack of support for something, and other times it's downright buggy.</p>
<p>An issue I came across recently, is something to do with Windows Authentication. There's a lot of resource on the web on how to run ASP.NET MVC with Windows Authentication. There's even a project template, the Intranet site, which ships with MVC 3. It should be a no-brainer!</p>
<p>The issue I came across, prevented ASP.NET from using Windows Authentication. Even though I had set authentication mode to Windows in Web.config:</p>
<pre class="prettyprint">&lt;authentication mode="Windows" /&gt;
&lt;authorization&gt;
  &lt;deny users="?" /&gt;
&lt;/authorization&gt;</pre>
<p>And disabled anonymous authentication on the website in IIS - it always redirected all requests to <em>/Account/Login</em>, as if it was using some sort of default Forms Authentication.</p>
<p>This coursed a server error - the dreaded Yellow Screen of Death, since I didn't have a login form on my site, I had no controller/action for Account/Login.</p>
<h2>Known issue</h2>
<p>After searching the web for a while, I came across the <a href="http://www.asp.net/learn/whitepapers/mvc3-release-notes">release notes</a> on the ASP.NET MVC website. At the bottom, there's a section called '<a href="http://www.asp.net/learn/whitepapers/mvc3-release-notes#0.1__Toc274034230">Known issues</a>', and one of the last issues reads:</p>
<blockquote><p>There’s a known issue that causes Forms Authentication to always redirect unauthenticated users to ~/Account/Login, ignoring the forms authentication setting used in Web.config. The workaround is to add the following app setting.</p>
<pre>      &lt;add key="autoFormsAuthentication" value="false" /&gt;</pre>
</blockquote>
<p>And much to my disappointment, this didn't do the trick! Long story short, it turned out that I needed another setting as well, as pointed out in the comment of <a href="http://stackoverflow.com/questions/4078525/problem-exclusively-using-windows-authentication-in-asp-net-mvc-3-beta">this Stackoverflow question</a>:</p>
<pre>&lt;add key="enableSimpleMembership" value="false"/&gt;</pre>
<p>After adding the magic settings to my Web.config, everything worked as it should - and Windows Authentication on ASP.NET MVC <em>is</em> now a no-brainer!</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/asp-net-mvc-3-windows-authentication-problem-redirects-to-account-login/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Making your ASP.NET Global Resource files work in JavaScript. IntelliSense included!</title>
		<link>http://martinnormark.com/making-your-asp-net-global-resource-files-work-in-javascript-intellisense-included?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/making-your-asp-net-global-resource-files-work-in-javascript-intellisense-included#comments</comments>
		<pubDate>Mon, 15 Nov 2010 15:22:20 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Internationalization]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://martinnormark.com/?p=56</guid>
		<description><![CDATA[Any modern web application needs localization! You simply can't ignore the huge amounts of people who doesn't speak your language, or whose native language is different from yours. You're probably using resource files (.resx) in .NET, but how do you &#8230; <p><a class="btn small" href="http://martinnormark.com/making-your-asp-net-global-resource-files-work-in-javascript-intellisense-included">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>Any modern web application needs localization! You simply can't ignore the huge amounts of people who doesn't speak your language, or whose native language is different from yours.</p>
<p>You're probably using resource files (.resx) in .NET, but how do you go about getting values from your resource files in JavaScript?</p>
<p><a href="http://www.west-wind.com/Weblog/default.aspx">Rick Strahl</a> wrote a great <a href="http://www.west-wind.com/Weblog/posts/698097.aspx">blog post about an HttpHandler</a> that serves the content of your resource files in JavaScript. You basically add a script tag to your page that points to the HttpHandler, and the HttpHandler will produce the resources in JavaScript as an object with properties on it, like this:</p>
<blockquote><p>
[code lang="js"]var localRes = {<br />
    AreYouSureYouWantToRemoveValue: &quot;Sind Sie sicher dass Sie diesen Wert l\u00F6schen wollen?&quot;,<br />
    BackupComplete: &quot;Der Backup f\u00FChrte erfolgreich durch&quot;,<br />
    BackupFailed: &quot;Der Backup konnte nicht durchgef\u00FChrt werden&quot;,<br />
    BackupNotification: &quot;Diese Operation macht einen Backup von der Lokalisationtabelle. \nM\u00F6chten Sie fortfahren?&quot;,<br />
    Close: &quot;Schliessen&quot;,<br />
    FeatureDisabled: &quot;Diese Funktion ist nicht vorhanden im on-line Demo &quot;,<br />
    InvalidFileUploaded: &quot;Unzul\u00E4ssige Akten Format hochgeladen.&quot;,<br />
    InvalidResourceId: &quot;Unzul\u00E4ssige ResourceId&quot;,<br />
    Loading: &quot;Laden&quot;,<br />
    LocalizationTableCreated: &quot;Lokalisations Akte wurde erfolgreich erstellt.&quot;,<br />
    LocalizationTableNotCreated: &quot;Die Localizations Akte konnte nicht erstellt werden.&quot;<br />
};[/code]</p></blockquote>
<p>This is awesome! Now you can access your resource files from JavaScript. Though, I see a single improvement to be made. The thing I don't like about the HttpHandler approach, is that you don't get IntelliSense support in Visual Studio. So you have to browse your resource file and copy &amp; paste the resource key to your JavaScript files in order for this to work.</p>
<h2>Generate static JavaScript files on Post-build</h2>
<p>Instead of generating dynamic files at runtime, I prefer to generate static JavaScript files and then dynamically include the file of the current language on my pages. I do this by calling a Console Application I’ve written on the Post-build event of my ASP.NET (MVC) project, which you can set in the property pages of your project (By the way, did you know that you can open property pages of a project by double clicking the default Properties folder?):</p>
<p><a href="http://martinnormark.com/wp-content/uploads/2010/11/image1.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" src="http://martinnormark.com/wp-content/uploads/2010/11/image_thumb1.png" border="0" alt="image" width="634" height="490" /></a></p>
<p>What my JavascriptResxGenerator App does, is quite messy. But the essence of the App is, of course, to take a single (or several) .resx files and do the following:</p>
<p>1. Loop through all keys.</p>
<p>2. Make sure the key is not a <a href="http://javascript.about.com/library/blreserved.htm">JavaScript reserved word</a>.</p>
<p>3. Add the key and value to a dictionary (in JavaScript).</p>
<p>4. Write the file.</p>
<p>And for the default culture I generate a –vsdoc file, that I can use for Visual Studio IntelliSense.</p>
<h2>Using the JavaScript Resx Generator App</h2>
<p>My App supports a single file approach, and directory approach.</p>
<p><strong>Single file: </strong>JavascriptResxGenerator.exe C:\Folder\Text.resx C:\Output C:\Output\VsDoc MyApp.Namespace.Resources</p>
<p><strong>Directory: </strong>JavascriptResxGenerator.exe C:\Folder C:\Output C:\Output\VsDoc MyApp.Namespace.Resources</p>
<p>The MyApp.Namespace.Resources value, is the namespace your resource dictionary will get enclosed in.</p>
<p><a href="http://martinnormark.com/wp-content/uploads/2010/11/image2.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" src="http://martinnormark.com/wp-content/uploads/2010/11/image_thumb2.png" border="0" alt="image" width="634" height="294" /></a></p>
<h2>Embedding the file on your pages</h2>
<p>To include the JavaScript resource file in your page, you simply add a script include tag that points to the correct language. The App will respect the region token used in your Resx files. So if you have a file called Text.da.resx, the JavaScript file generated will include .da.resx at the end. It’s then up to you to add the correct logic to keep hold of the current language and specify the correct region token in order to include the correct JavaScript resource file.</p>
<h2>Download</h2>
<p>You can <a href="http://cid-146a3db7b364e824.office.live.com/self.aspx/Public/JavascriptResxGenerator.zip">download the App here</a>, as a ZIP file.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/making-your-asp-net-global-resource-files-work-in-javascript-intellisense-included/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>ASP.NET MVC View Page Editor in Visual Studio 2010 Beta 2 won’t recognize HTML tags</title>
		<link>http://martinnormark.com/asp-net-mvc-view-page-editor-in-visual-studio-2010-beta-2-won%e2%80%99t-recognize-html-tags?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/asp-net-mvc-view-page-editor-in-visual-studio-2010-beta-2-won%e2%80%99t-recognize-html-tags#comments</comments>
		<pubDate>Wed, 21 Oct 2009 13:24:00 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">/post/ASPNET-MVC-View-Page-Editor-in-Visual-Studio-2010-Beta-2-wone28099t-recognize-HTML-tags.aspx</guid>
		<description><![CDATA[I love the new IntelliSense dialog in Visual Studio 2010 Beta 2, that ScottGu has blogged about. But when editing an ASP.NET MVC View Page in Visual Studio 2010 Beta 2, you end up fighting the IntelliSense. That is because &#8230; <p><a class="btn small" href="http://martinnormark.com/asp-net-mvc-view-page-editor-in-visual-studio-2010-beta-2-won%e2%80%99t-recognize-html-tags">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>I love the new IntelliSense dialog in Visual Studio 2010 Beta 2, that <a href="http://weblogs.asp.net/scottgu/archive/2009/10/22/vs-2010-code-intellisense-improvements-vs-2010-and-net-4-0-series.aspx">ScottGu has blogged about</a>. But when editing an ASP.NET MVC View Page in Visual Studio 2010 Beta 2, you end up fighting the IntelliSense. That is because IntelliSense and the HTML Editor doesn’t recognize *any* HTML tags.</p>
<p><a href="http://martinnormark.com/image.axd?picture=image_1.png"><img style="display: inline; border-width: 0px;" title="image" src="http://martinnormark.com/image.axd?picture=image_thumb_1.png" border="0" alt="image" width="655" height="395" /></a></p>
<p>Trying to add a simple paragraph tag in HTML suggests a panel (code snippet). When you finish the P tag, by typing &gt;, the result is this:</p>
<p><a href="http://martinnormark.com/image.axd?picture=image_2.png"><img style="display: inline; border-width: 0px;" title="image" src="http://martinnormark.com/image.axd?picture=image_thumb_2.png" border="0" alt="image" width="306" height="188" /></a></p>
<p>Pretty annoying to fight with IntelliSense when you want to code! I thought you might be able to fix this, by deleting all the new ASP.NET specific code snippets in the “C:\Program Files\Microsoft Visual Studio 10.0\Web\Snippets\HTML\1033\ASP.NET” folder.</p>
<p>This actually works. Now you can actually see the ASP.NET MVC code snippets, that was hidden in the masses of the ASP.NET ones.</p>
<p><a href="http://martinnormark.com/image.axd?picture=image_3.png"><img style="display: inline; border-width: 0px;" title="image" src="http://martinnormark.com/image.axd?picture=image_thumb_3.png" border="0" alt="image" width="642" height="359" /></a></p>
<p>But it still has all the ASP.NET Server Controls listed. I thought I could get rid of these by removing a namespace reference to System.Web in the Pages section of Web.config. But that is not present. So I don’t know how to fix that.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/asp-net-mvc-view-page-editor-in-visual-studio-2010-beta-2-won%e2%80%99t-recognize-html-tags/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

