<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>ASP.NET on InternetException</title><link>https://internetexception.com/tags/asp.net/</link><description>Recent content in ASP.NET on InternetException</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Mon, 27 Feb 2023 14:04:19 +0100</lastBuildDate><atom:link href="https://internetexception.com/tags/asp.net/index.xml" rel="self" type="application/rss+xml"/><item><title>Difference between Controller and ControllerBase</title><link>https://internetexception.com/2022/10/25/difference-between-controller-and-controllerbase/</link><pubDate>Tue, 25 Oct 2022 16:28:23 +0200</pubDate><guid>https://internetexception.com/2022/10/25/difference-between-controller-and-controllerbase/</guid><description>&lt;ul&gt;&#10;&lt;li&gt;&lt;code&gt;Controller&lt;/code&gt; type inherits from &lt;code&gt;ControllerBase&lt;/code&gt; and have Views support&lt;/li&gt;&#10;&lt;li&gt;&lt;code&gt;ControllerBase&lt;/code&gt; type is better suited for APIs&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Further read:&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-6.0" target="_blank" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-6.0&lt;/a&gt;&#10;&lt;/p&gt;</description></item><item><title>Kestrel</title><link>https://internetexception.com/2022/10/25/kestrel/</link><pubDate>Tue, 25 Oct 2022 15:56:40 +0200</pubDate><guid>https://internetexception.com/2022/10/25/kestrel/</guid><description>&lt;ul&gt;&#10;&lt;li&gt;default application server for .net&lt;/li&gt;&#10;&lt;li&gt;cross platform&lt;/li&gt;&#10;&lt;li&gt;best performance and memory utilization&lt;/li&gt;&#10;&lt;li&gt;may not have some advanced features of HTTP.sys on Windows&lt;/li&gt;&#10;&lt;li&gt;may be used with proxy server or as internet facing server&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Further read:&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-6.0" target="_blank" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-6.0&lt;/a&gt;&#10;&lt;/p&gt;</description></item><item><title>ASP.NET Core controller level exception handling</title><link>https://internetexception.com/2022/10/25/asp-net-core-controller-level-exception-handling/</link><pubDate>Tue, 25 Oct 2022 15:53:52 +0200</pubDate><guid>https://internetexception.com/2022/10/25/asp-net-core-controller-level-exception-handling/</guid><description>&lt;p&gt;To handle exceptions on Controller/Action level Exception Filters are used.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-csharp" data-lang="csharp"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="na"&gt;[TypeFilter(typeof(SampleExceptionFilter))]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ExceptionController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Controller&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Exception Filters are implemented using &lt;code&gt;IExceptionFilter&lt;/code&gt; with &lt;code&gt;OnExcetion&lt;/code&gt; method override to handle exceptions.&lt;/p&gt;&#10;&lt;p&gt;Exception Filters can be applied to Controller and Action level.&lt;/p&gt;&#10;&lt;p&gt;Further read:&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-7.0#exception-filters" target="_blank" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-7.0#exception-filters&lt;/a&gt;&#10;&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.filters.iexceptionfilter?view=aspnetcore-7.0" target="_blank" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.filters.iexceptionfilter?view=aspnetcore-7.0&lt;/a&gt;&#10;&lt;/p&gt;</description></item><item><title>ASP.NET Core global exception handling</title><link>https://internetexception.com/2022/10/25/asp-net-core-global-exception-handling/</link><pubDate>Tue, 25 Oct 2022 15:49:53 +0200</pubDate><guid>https://internetexception.com/2022/10/25/asp-net-core-global-exception-handling/</guid><description>&lt;p&gt;To add global exception handling in ASP.NET Core just use configuration method:&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;app.UseExceptionHandler&lt;/code&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-6.0#exception-handler-lambda" target="_blank" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-6.0#exception-handler-lambda&lt;/a&gt;&#10;&lt;/p&gt;</description></item><item><title>What ASP.NET Middleware can do?</title><link>https://internetexception.com/2022/10/25/what-asp-net-middleware-can-do/</link><pubDate>Tue, 25 Oct 2022 15:45:12 +0200</pubDate><guid>https://internetexception.com/2022/10/25/what-asp-net-middleware-can-do/</guid><description>&lt;p&gt;Pretty much everything. Default ASP.NET Core request handling pipeline is consisting of bunch of middlewares:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-6.0" target="_blank" rel="noopener noreferrer"&gt;Authentication&lt;/a&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.authorizationappbuilderextensions.useauthorization" target="_blank" rel="noopener noreferrer"&gt;Authorization&lt;/a&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-6.0" target="_blank" rel="noopener noreferrer"&gt;CORS&lt;/a&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-6.0" target="_blank" rel="noopener noreferrer"&gt;HTTP Logging&lt;/a&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-6.0" target="_blank" rel="noopener noreferrer"&gt;MVC&lt;/a&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0" target="_blank" rel="noopener noreferrer"&gt;Static Files&lt;/a&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;other&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Whole list of built-in middlewares can be found here:&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-7.0#built-in-middleware" target="_blank" rel="noopener noreferrer"&gt;https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-7.0#built-in-middleware&lt;/a&gt;&#10;&lt;/p&gt;&#10;&lt;p&gt;There is possibility to write custom middleware too.&lt;/p&gt;</description></item><item><title>Middleware in OWIN</title><link>https://internetexception.com/2022/10/25/middleware-in-owin/</link><pubDate>Tue, 25 Oct 2022 15:40:45 +0200</pubDate><guid>https://internetexception.com/2022/10/25/middleware-in-owin/</guid><description>&lt;p&gt;There are 2 ways of writing OWIN middleware in ASP.NET Core.&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Pure OWIN - usefull if you want to integrate with OWIN compatible module but not written in C#&lt;/li&gt;&#10;&lt;li&gt;ASP.NET Core OWIN middleware&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="aspnet-core-middleware"&gt;ASP.NET Core Middleware:&lt;/h2&gt;&#10;&lt;p&gt;This type of middleware can be:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;delegate of &lt;code&gt;Func&amp;lt;HttpContext, Func&amp;lt;&lt;/code&gt;Task&amp;gt;&lt;code&gt;, Task&amp;gt;&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;class with &lt;a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.imiddleware?view=aspnetcore-7.0" data-type="URL" data-id="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.imiddleware?view=aspnetcore-7.0"&gt;&lt;code&gt;IMiddleware&lt;/code&gt;&lt;/a&gt; imlementation&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-csharp" data-lang="csharp"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// Do work that can write to the Response.&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Invoke&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// Do logging or other work that doesn&amp;#39;t write to the Response.&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;});&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="pure-owin-middleware"&gt;Pure OWIN Middleware:&lt;/h2&gt;&#10;&lt;p&gt;This rather would be used to integrate with OWIN middlewares written in different language, so that ASP.NET Core implementation directly is not possible.&lt;/p&gt;</description></item></channel></rss>