<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Aspnet on Brandon Pugh&#39;s Blog</title>
    <link>https://www.brandonpugh.com/tags/aspnet/</link>
    <description>Recent content in Aspnet on Brandon Pugh&#39;s Blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Mon, 28 Oct 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://www.brandonpugh.com/tags/aspnet/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>View ModelState errors while debugging</title>
      <link>https://www.brandonpugh.com/til/aspnet/view-modelstate-errors/</link>
      <pubDate>Mon, 28 Oct 2024 00:00:00 +0000</pubDate>
      <guid>https://www.brandonpugh.com/til/aspnet/view-modelstate-errors/</guid>
      <description>&lt;p&gt;Today I learned how you can view the Modelstate errors while debugging a controller in Asp.Net:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-csharp&#34; data-lang=&#34;csharp&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;n&#34;&gt;ModelState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Where&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Value&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Errors&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Count&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;m&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Select&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;x&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;x&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;Key&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ToList&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Just paste that in the watch window.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;ve tried debugging to find what field is causing the Modelstate to be invalid, you&amp;rsquo;ll know how tedious it is to dig through but thanks to &lt;a href=&#34;https://windowshell.wordpress.com/2016/06/11/realtime-mvc-modelstate-errors-during-debugging/&#34;&gt;this post&lt;/a&gt; for this handy tip!&lt;/p&gt;

       &lt;hr&gt; &lt;p&gt;Thank you for keeping RSS alive. You&#39;re awesome.&lt;/p&gt; &lt;p&gt;&lt;a href=&#34;mailto:blogrss@bpugh.dev&#34;&gt;Reply by email&lt;/a&gt;&lt;/p&gt;
        &lt;img src=&quot;https://blog.bpugh.workers.dev/cdn/images?p=/til/aspnet/view-modelstate-errors/feed&quot;&gt;
      </description>
    </item>
    
    <item>
      <title>ASP.NET max upload file size</title>
      <link>https://www.brandonpugh.com/til/aspnet/max-upload-file-size/</link>
      <pubDate>Fri, 30 Aug 2024 00:00:00 +0000</pubDate>
      <guid>https://www.brandonpugh.com/til/aspnet/max-upload-file-size/</guid>
      <description>&lt;p&gt;Today I learned there are &lt;em&gt;two&lt;/em&gt; configuration values that determine how large of a file can be uploaded.
&lt;code&gt;maxRequestLength&lt;/code&gt; is specified in &lt;strong&gt;Kilobytes&lt;/strong&gt; and it&amp;rsquo;s used by the ASP.NET framework.
If the file is larger than this value then the application will throw &amp;ldquo;content length exceed&amp;rdquo; exception so it just comes back as a 500 which isn&amp;rsquo;t that helpful.
The default is about 4mb.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;maxAllowedContentLength&lt;/code&gt; is used by IIS and is specified in &lt;strong&gt;bytes&lt;/strong&gt; not kilobytes and if the file is larger then it will return 413 error status which we can handle appropriately.
The default is about 28mb.&lt;/p&gt;
&lt;p&gt;Both of these values need to set to larger than the file size we want to allow but in our case we needed the &lt;code&gt;maxAllowedContentLength&lt;/code&gt; to be the smaller value so that an upload that&amp;rsquo;s too large will always get rejected by IIS first so we can display a useful error message to the user.&lt;/p&gt;
&lt;p&gt;The issue we ran into was if the file was more than 4mb but less than 28mb, it was reject by aspnet and throwing a 500 which in production comes back as a 302 redirect to the error page which we weren&amp;rsquo;t handling so it would look like the upload was just hanging.&lt;/p&gt;
&lt;p&gt;If the file was larger than 28mb then it would get rejected by IIS and return a 413 which we &lt;em&gt;were&lt;/em&gt; handling so we would display the correct &amp;ldquo;File is too large&amp;rdquo; error message.&lt;/p&gt;
&lt;p&gt;There are even more ways to limit upload size you read about in Kahlid&amp;rsquo;s post: &lt;a href=&#34;https://khalidabuhakmeh.com/increase-file-upload-limit-for-aspdotnet&#34;&gt;increase file upload limit&lt;/a&gt;&lt;/p&gt;

       &lt;hr&gt; &lt;p&gt;Thank you for keeping RSS alive. You&#39;re awesome.&lt;/p&gt; &lt;p&gt;&lt;a href=&#34;mailto:blogrss@bpugh.dev&#34;&gt;Reply by email&lt;/a&gt;&lt;/p&gt;
        &lt;img src=&quot;https://blog.bpugh.workers.dev/cdn/images?p=/til/aspnet/max-upload-file-size/feed&quot;&gt;
      </description>
    </item>
    
  </channel>
</rss>