<?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/category/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>How to Send HTML E-mail from an ASP.NET MVC Controller</title>
		<link>http://martinnormark.com/actionmailer-net-html-email?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://martinnormark.com/actionmailer-net-html-email#comments</comments>
		<pubDate>Sat, 02 Apr 2011 22:04:54 +0000</pubDate>
		<dc:creator>Martin Normark</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[email]]></category>

		<guid isPermaLink="false">http://martinnormark.com/actionmailer-net-html-email</guid>
		<description><![CDATA[As I argued about a year ago, any modern piece of software needs to send e-mail. It needs to connect, and reach its users. Back then I showed how to send templated HTML e-mail, using the little known MailDefinition class &#8230; <p><a class="btn small" href="http://martinnormark.com/actionmailer-net-html-email">Continue reading <span class="meta-nav">&#8594;</span></a></p>]]></description>
			<content:encoded><![CDATA[<p>As I argued about a year ago, any <strong>modern piece of software needs </strong><a href="http://martinnormark.com/generate-html-e-mail-body-in-c-using-templates"><strong>to send e-mail</strong></a>. It needs to connect, and reach its users. Back then I showed <a href="http://martinnormark.com/generate-html-e-mail-body-in-c-using-templates">how to send templated HTML e-mail</a>, using the little known MailDefinition class in C#.</p>
<p>Today, there’s an even better approach. The main problem with the old solution, was that the templates were plain, old, flat and boring HTML templates. Any sort of dynamic content, were limited to what a simple replace could do.</p>
<p>The better approach that I’m using now, is the awesome <a href="https://bitbucket.org/swaj/actionmailer.net/wiki/Home">ActionMailer.NET</a> package. It’s right there within Visual Studio, if you have <a href="http://nuget.codeplex.com/">Nuget</a> – which I’m sure you do.</p>
<h2>Why is it better?</h2>
<p><a href="https://bitbucket.org/swaj/actionmailer.net/wiki/Home">ActionMailer.NET</a> is better, first of all because the templates for your e-mails are just <strong>regular ASP.NET MVC views</strong>. That means you can strongly type those view and supply it with a model. All the stuff you can do in an ASP.NET MVC View (Razor or WebForms – your choice) – you can now send via E-mail.</p>
<h2>It’s very easy to use</h2>
<p>You simply start by creating a new Controller in your ASP.NET MVC web application that will take care of sending all the e-mails. Along with the controller, you add a folder for the views.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MailController : MailerBase
{
    <span class="kwrd">public</span> EmailResult VerificationEmail(User model)
    {
        To.Add(model.EmailAddress);
        From = <span class="str">&quot;Martin Normark &lt;mysite@email.com&gt;&quot;</span>;
        Subject = <span class="str">&quot;Welcome to My Blog!&quot;</span>;

        <span class="kwrd">return</span> Email(<span class="str">&quot;VerificationEmail&quot;</span>, model);
    }
}</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Notice how you can set the from name, as well as the e-mail address. Neat stuff. If you want to do something just before or after an e-mail is sent, you can play around with the OnMailSending and OnMailSent by overriding the default implementation.</p>
<pre class="csharpcode"><span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnMailSending(MailSendingContext context)
{
    <span class="kwrd">base</span>.OnMailSending(context);
}

<span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnMailSent(System.Net.Mail.MailMessage mail)
{
    <span class="kwrd">base</span>.OnMailSent(mail);
}
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
</pre>
<p>Maybe you want to save the e-mail in your database for future reference, logging etc.</p>
<p>One thing I’ll do one day, is combine this with the <a href="https://github.com/alexdunae/premailer">premailer Ruby project</a>, that optimizes your HTML e-mails for rendering in e-mail clients, by moving your CSS inline.</p>
]]></content:encoded>
			<wfw:commentRss>http://martinnormark.com/actionmailer-net-html-email/feed</wfw:commentRss>
		<slash:comments>1</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>

