<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MBARK T3STO</title>
    <description>The latest articles on DEV Community by MBARK T3STO (@mbarkt3sto).</description>
    <link>https://dev.to/mbarkt3sto</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F576456%2Fc9d2e04f-786f-403e-8b78-c7160e44b26c.jpg</url>
      <title>DEV Community: MBARK T3STO</title>
      <link>https://dev.to/mbarkt3sto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mbarkt3sto"/>
    <language>en</language>
    <item>
      <title>How to Read Action Method Parameters using IActionFilter in ASP.NET Core MVC</title>
      <dc:creator>MBARK T3STO</dc:creator>
      <pubDate>Sat, 07 Jan 2023 20:02:36 +0000</pubDate>
      <link>https://dev.to/mbarkt3sto/how-to-read-action-method-parameters-using-iactionfilter-in-aspnet-core-mvc-fej</link>
      <guid>https://dev.to/mbarkt3sto/how-to-read-action-method-parameters-using-iactionfilter-in-aspnet-core-mvc-fej</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlduh3jz4g1y9npufb9a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlduh3jz4g1y9npufb9a.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to IActionFilter in ASP.NET Core MVC
&lt;/h2&gt;

&lt;p&gt;ASP.NET Core MVC is a powerful framework for building web applications. One of the features of ASP.NET Core MVC is the ability to use filters to modify the behavior of action methods. IActionFilter is an interface that can be implemented to create a filter that runs before or after an action method is executed.&lt;/p&gt;

&lt;p&gt;In this article, we will learn how to read action method parameters using IActionFilter in ASP.NET Core MVC.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing IActionFilter
&lt;/h2&gt;

&lt;p&gt;To implement IActionFilter, we need to create a class that implements the interface and overrides the OnActionExecuting and OnActionExecuted methods. The OnActionExecuting method is called before the action method is executed, and the OnActionExecuted method is called after the action method is executed.&lt;/p&gt;

&lt;p&gt;Here is an example of a filter that implements IActionFilter:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyActionFilter&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IActionFilter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutingContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Code to run before the action method is executed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutedContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Code to run after the action method is executed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Reading Action Method Parameters
&lt;/h2&gt;

&lt;p&gt;To read action method parameters using IActionFilter, we can access the ActionArguments property of the ActionExecutingContext object in the OnActionExecuting method. The ActionArguments property is a dictionary that contains the names and values of the parameters of the action method.&lt;/p&gt;

&lt;p&gt;Here is examples of how to read action method parameters using IActionFilter:&lt;/p&gt;
&lt;h2&gt;
  
  
  Using ActionArguments
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyActionFilter&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IActionFilter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutingContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;parameter1&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;ActionArguments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"parameter1"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;parameter2&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;ActionArguments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"parameter2"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutedContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Code to run after the action method is executed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this example, we are accessing the values of the "parameter1" and "parameter2" parameters of the action method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using IActionFilter in an Action Method
&lt;/h2&gt;

&lt;p&gt;To use an IActionFilter in an action method, we need to apply the filter to the action method using the [Filter] attribute.&lt;/p&gt;

&lt;p&gt;Using TryGetValue method&lt;br&gt;
Using the TryGetValue method of the ActionArguments property of the ActionExecutingContext object can be useful when we want to read an optional parameter of an action method. The TryGetValue method attempts to get the value of the specified parameter from the ActionArguments dictionary, and returns a boolean value indicating whether the value was found.&lt;/p&gt;

&lt;p&gt;Here is an example of how to use the TryGetValue method to read an optional parameter of an action method:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyActionFilter&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IActionFilter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutingContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&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;ActionArguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TryGetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"parameter1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// parameter1 value was found&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// parameter1 value was not found&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutedContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Code to run after the action method is executed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this example, we are using the TryGetValue method to try to get the value of the "parameter1" parameter of the action method. If the value is found, the parameter1 variable is set to the value of the parameter, and the code in the if block is executed. If the value is not found, the parameter1 variable is not set and the code in the else block is executed.&lt;/p&gt;

&lt;p&gt;This can be useful when we have an action method with optional parameters that may or may not be provided by the client. By using the TryGetValue method, we can handle both cases and take appropriate action based on whether the parameter value was found or not.&lt;/p&gt;

&lt;p&gt;Here is an example of how to apply an IActionFilter to an action method:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MyActionFilter&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IActionResult&lt;/span&gt; &lt;span class="nf"&gt;MyAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;parameter2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Action method code&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this example, we are applying the MyActionFilter filter to the MyAction action method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real examples
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Logger Filter
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LogActionFilter&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IActionFilter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="n"&gt;ILogger&lt;/span&gt; &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;LogActionFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ILogger&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;LogActionFilter&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_logger&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutingContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Log the action method name and parameters&lt;/span&gt;
        &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Executing action method {ActionName} with parameters:"&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;ActionDescriptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DisplayName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt; &lt;span class="k"&gt;in&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;ActionArguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LogInformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{ParameterName}: {ParameterValue}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parameter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutedContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Code to run after the action method is executed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this example, we have created a LogActionFilter filter that logs the name and parameters of the action method before it is executed. The filter uses the ILogger service to log the information, and the ActionArguments property of the ActionExecutingContext object to access the action method parameters.&lt;/p&gt;

&lt;p&gt;To use this filter, we can apply it to an action method using the [Filter] attribute:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;LogActionFilter&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IActionResult&lt;/span&gt; &lt;span class="nf"&gt;MyAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;parameter2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Action method code&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When the MyAction action method is executed, the LogActionFilter filter will log the name and parameters of the action method before the action method code is executed. This can be useful for logging purposes or for debugging.&lt;/p&gt;

&lt;h2&gt;
  
  
  Validation Filter
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ValidateActionFilter&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IActionFilter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutingContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&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;ActionArguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TryGetValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"parameter1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="n"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;))&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;Result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;BadRequestObjectResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Parameter1 cannot be empty."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;OnActionExecuted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ActionExecutedContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Code to run after the action method is executed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this example, we have created a ValidateActionFilter filter that validates the "parameter1" parameter of the action method. If the parameter is empty or null, the filter sets the Result property of the ActionExecutingContext object to a BadRequestObjectResult object with an error message. This will cause the action method to return a Bad Request response to the client.&lt;/p&gt;

&lt;p&gt;To use this filter, we can apply it to an action method using the [Filter] attribute:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ValidateActionFilter&lt;/span&gt;&lt;span class="p"&gt;))]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IActionResult&lt;/span&gt; &lt;span class="nf"&gt;MyAction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;parameter2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Action method code&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;View&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When the MyAction action method is executed, the ValidateActionFilter filter will validate the "parameter1" parameter before the action method code is executed. If the parameter is empty or null, the filter will return a Bad Request response to the client, and the action method code will not be executed. This can be useful for input validation purposes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we learned how to read action method parameters using IActionFilter in ASP.NET Core MVC. We saw how to implement IActionFilter and how to access the action method parameters in the OnActionExecuting method. We also saw how to apply an IActionFilter to an action method using the [Filter] attribute.&lt;/p&gt;

&lt;p&gt;Using IActionFilter, we can modify the behavior of action methods and access the values of their parameters. This can be useful for a variety of purposes, such as logging, validation, and authorization.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>asp</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>How to Split services registrations in .Net core DI using Extension Methods?</title>
      <dc:creator>MBARK T3STO</dc:creator>
      <pubDate>Sat, 24 Dec 2022 19:07:25 +0000</pubDate>
      <link>https://dev.to/mbarkt3sto/how-to-split-services-registrations-in-net-core-di-using-extension-methods-4lc7</link>
      <guid>https://dev.to/mbarkt3sto/how-to-split-services-registrations-in-net-core-di-using-extension-methods-4lc7</guid>
      <description>&lt;p&gt;In .NET Core, the built-in dependency injection (DI) container makes it easy to register and resolve dependencies in your applications. You can use the container to manage the lifetime of your dependencies and inject them into your classes as needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Splitting Services Registrations in .NET Core DI&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;As your application grows, you may find that your DI container becomes cluttered with a large number of service registrations. To help manage this complexity, you can use extension methods to split your service registrations into smaller, more manageable chunks.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Creating an Extension Method&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To create an extension method, you need to create a static class with one or more static methods that have the &lt;code&gt;this&lt;/code&gt; keyword in their parameter list. For example, consider the following extension method for registering a collection of services:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddMyServices(this IServiceCollection services)
    {
        services.AddTransient&amp;lt;IMyService, MyService&amp;gt;();
        services.AddTransient&amp;lt;IAnotherService, AnotherService&amp;gt;();
        // Add additional service registrations here...

        return services;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This extension method can be called on an &lt;code&gt;IServiceCollection&lt;/code&gt; instance to add a collection of services to the DI container.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Using the Extension Method&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To use the extension method, you can call it on the &lt;code&gt;IServiceCollection&lt;/code&gt; instance in your application's &lt;code&gt;Startup&lt;/code&gt; class. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
    // Add other service registrations here...
    services.AddMyServices();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using extension methods to split your service registrations into smaller chunks, you can make your DI container more organized and easier to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Example: Splitting Services by Feature&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;One common way to split service registrations is by feature or module. For example, consider a simple application with two features: &lt;code&gt;FeatureA&lt;/code&gt; and &lt;code&gt;FeatureB&lt;/code&gt;. You could create separate extension methods for each feature, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddFeatureAServices(this IServiceCollection services)
    {
        services.AddTransient&amp;lt;IFeatureAService, FeatureAService&amp;gt;();
        // Add additional Feature A service registrations here...

        return services;
    }

    public static IServiceCollection AddFeatureBServices(this IServiceCollection services)
    {
        services.AddTransient&amp;lt;IFeatureBService, FeatureBService&amp;gt;();
        // Add additional Feature B service registrations here...

        return services;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in the &lt;code&gt;ConfigureServices&lt;/code&gt; method of the &lt;code&gt;Startup&lt;/code&gt; class, you can call the appropriate extension method for each feature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
    // Add other service registrations here...
    services.AddFeatureAServices();
    services.AddFeatureBServices();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Example: Splitting Services by Infrastructure&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Another common way to split service registrations is by infrastructure or cross-cutting concern. For example, you might have a set of services that are related to logging, another set related to caching, and another set related to security. You could create separate extension methods for each infrastructure layer, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddLoggingServices(this IServiceCollection services)
    {
        services.AddSingleton&amp;lt;ILoggerFactory, LoggerFactory&amp;gt;();
        services.AddSingleton(typeof(ILogger&amp;lt;&amp;gt;), typeof(Logger&amp;lt;&amp;gt;));
        // Add additional logging service registrations here...

        return services;
    }

    public static IServiceCollection AddCachingServices(this IServiceCollection services)
    {
        services.AddSingleton&amp;lt;ICacheManager, CacheManager&amp;gt;();
        // Add additional caching service registrations here...

        return services;
    }

    public static IServiceCollection AddSecurityServices(this IServiceCollection services)
    {
        services.AddTransient&amp;lt;ITokenService, TokenService&amp;gt;();
        // Add additional security service registrations here...

        return services;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, in the &lt;code&gt;ConfigureServices&lt;/code&gt; method of the &lt;code&gt;Startup&lt;/code&gt; class, you can call the appropriate extension method for each infrastructure layer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
    // Add other service registrations here...
    services.AddLoggingServices();
    services.AddCachingServices();
    services.AddSecurityServices();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach allows you to easily manage and maintain the different infrastructure layers of your application separately.&lt;/p&gt;

&lt;p&gt;Splitting service registrations in .NET Core DI using extension methods can help improve the maintainability and organization of your application's DI container. By dividing your registrations into smaller, more manageable chunks, you can more easily manage the dependencies of your application and make it easier to change or replace components at runtime.&lt;/p&gt;

</description>
      <category>csharp</category>
    </item>
    <item>
      <title>What is the Difference between ViewBag and ViewData in ASP.NET Core MVC</title>
      <dc:creator>MBARK T3STO</dc:creator>
      <pubDate>Sat, 24 Dec 2022 19:01:29 +0000</pubDate>
      <link>https://dev.to/mbarkt3sto/what-is-the-difference-between-viewbag-and-viewdata-in-aspnet-core-mvc-48cj</link>
      <guid>https://dev.to/mbarkt3sto/what-is-the-difference-between-viewbag-and-viewdata-in-aspnet-core-mvc-48cj</guid>
      <description>&lt;p&gt;&lt;a href="http://ASP.NET" rel="noopener noreferrer"&gt;ASP.NET&lt;/a&gt; Core is a popular framework for building web applications using the .NET platform. Within &lt;a href="http://ASP.NET" rel="noopener noreferrer"&gt;ASP.NET&lt;/a&gt; Core, there are two key mechanisms for transferring data from a controller to a view: ViewBag and ViewData. While these mechanisms serve a similar purpose, they have some notable differences that developers should be aware of.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;ViewBag&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;ViewBag is a dynamic property that is available on the base controller class in &lt;a href="http://ASP.NET" rel="noopener noreferrer"&gt;ASP.NET&lt;/a&gt; Core. It allows you to pass data from a controller to a view by assigning values to properties on the ViewBag object. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public IActionResult Index()
{
    ViewBag.Message = "Hello, World!";
    return View();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the view, you can access the value of the &lt;code&gt;Message&lt;/code&gt; property using the &lt;code&gt;@ViewBag&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ViewBag.Message

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One advantage of ViewBag is that it is dynamically typed, which means that you don't need to specify the type of the data being passed. This can make it easier to use in some cases, as you don't need to worry about type casting.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;ViewData&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Like ViewBag, ViewData is a mechanism for passing data from a controller to a view. However, ViewData is a dictionary object that stores key-value pairs, rather than a dynamic property.&lt;/p&gt;

&lt;p&gt;Here is an example of using ViewData to pass data from a controller to a view:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public IActionResult Index()
{
    ViewData["Message"] = "Hello, World!";
    return View();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the view, you can access the value of the &lt;code&gt;Message&lt;/code&gt; key using the &lt;code&gt;ViewData&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ViewData["Message"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike ViewBag, ViewData is strongly typed, which means that you need to specify the type of the data being passed. This can make it more error-prone, as you need to be careful about type casting. However, it can also make your code easier to maintain, as the type of the data is explicit and can be checked at compile time.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Summary&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In summary, ViewBag and ViewData are both mechanisms for transferring data from a controller to a view in &lt;a href="http://ASP.NET" rel="noopener noreferrer"&gt;ASP.NET&lt;/a&gt; Core. ViewBag is a dynamic property that is easy to use, but not strongly typed, while ViewData is a dictionary object that is strongly typed but requires more explicit type casting.&lt;/p&gt;

&lt;p&gt;Which one you choose will depend on your specific needs and preferences. Both ViewBag and ViewData can be useful in different situations, so it's important to understand the differences between them and how they work.&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>ai</category>
      <category>azure</category>
      <category>cloud</category>
    </item>
    <item>
      <title>How to Generate Lower-Case URLs in ASP.NET Core?</title>
      <dc:creator>MBARK T3STO</dc:creator>
      <pubDate>Sat, 24 Dec 2022 18:57:26 +0000</pubDate>
      <link>https://dev.to/mbarkt3sto/how-to-generate-lower-case-urls-in-aspnet-core-a85</link>
      <guid>https://dev.to/mbarkt3sto/how-to-generate-lower-case-urls-in-aspnet-core-a85</guid>
      <description>&lt;p&gt;In this article, we will look at how to generate lower-case URLs in &lt;a href="http://ASP.NET" rel="noopener noreferrer"&gt;ASP.NET&lt;/a&gt; Core using the built-in routing features.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Configuring the Routing Middleware&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In order to generate lower-case URLs in &lt;a href="http://ASP.NET" rel="noopener noreferrer"&gt;ASP.NET&lt;/a&gt; Core, you will need to use the routing middleware. This middleware is responsible for mapping incoming requests to the appropriate controller and action in your application.&lt;/p&gt;

&lt;p&gt;To configure the routing middleware, open the &lt;code&gt;Startup.cs&lt;/code&gt; file and add the following code to the &lt;code&gt;Configure&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseRouting&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will enable the routing middleware in your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Adding a Route&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Next, you will need to add a route to your application. A route is a pattern that specifies the URL structure of your application.&lt;/p&gt;

&lt;p&gt;To add a route, you can use the &lt;code&gt;MapRoute&lt;/code&gt; method of the &lt;code&gt;IRouteBuilder&lt;/code&gt; interface. This method takes two arguments: a string that specifies the route pattern, and an &lt;code&gt;Action&amp;lt;RouteBuilder&amp;gt;&lt;/code&gt; delegate that configures the route.&lt;/p&gt;

&lt;p&gt;Here is an example of how to add a route to your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseEndpoints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoints&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;endpoints&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;MapControllerRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"{controller=Home}/{action=Index}/{id?}"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This route will match URLs that have a controller and an action specified, such as &lt;code&gt;/home/index&lt;/code&gt;. The &lt;code&gt;{id?}&lt;/code&gt; parameter is optional and will match any additional segments in the URL.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Generating Lower-Case URLs&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To generate lower-case URLs, you can use the &lt;code&gt;LowercaseUrls&lt;/code&gt; property of the &lt;code&gt;RouteOptions&lt;/code&gt; class. This property is a boolean value that specifies whether the generated URLs should be in lower case or not.&lt;/p&gt;

&lt;p&gt;To configure the &lt;code&gt;LowercaseUrls&lt;/code&gt; property, you can use the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UseEndpoints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoints&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;endpoints&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;MapControllerRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"default"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"{controller=Home}/{action=Index}/{id?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;defaults&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;constraints&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;dataTokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;RouteOptions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;LowercaseUrls&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate lower-case URLs for all routes in your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Testing the Lower-Case URLs&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To test the lower-case URLs, you can run your application using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet run

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will start the application and you can navigate to it in a web browser.&lt;/p&gt;

&lt;p&gt;To test the lower-case URLs, you can try navigating to a URL with mixed case, such as &lt;a href="http://localhost:5000/Home/Index" rel="noopener noreferrer"&gt;&lt;code&gt;http://localhost:5000/Home/Index&lt;/code&gt;&lt;/a&gt;. You should see that the URL is automatically converted to lower case when it is displayed in the address bar.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In this article, we looked at how to generate lower-case URLs in &lt;a href="http://ASP.NET" rel="noopener noreferrer"&gt;ASP.NET&lt;/a&gt; Core using the routing middleware. We saw how to configure the &lt;code&gt;LowercaseUrls&lt;/code&gt; property of the &lt;code&gt;RouteOptions&lt;/code&gt; class to generate lower-case URLs for all routes in our application.&lt;/p&gt;

&lt;p&gt;By following these steps, you can ensure that the URLs in your &lt;a href="http://ASP.NET" rel="noopener noreferrer"&gt;ASP.NET&lt;/a&gt; Core application are all in lower case, which can help with SEO and improve the user experience.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>help</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
