﻿<?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/"
	>

<channel>
	<title>Programming By A Tool &#187; Attributes</title>
	<atom:link href="http://byatool.com/tag/attributes/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>ASP.Net MVC: Attibute to Check if Route Value Exists and&#8230; and Means Something!</title>
		<link>http://byatool.com/pointless/asp-net-mvc-attibute-to-check-if-route-value-exists-and-and-means-something/</link>
		<comments>http://byatool.com/pointless/asp-net-mvc-attibute-to-check-if-route-value-exists-and-and-means-something/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 12:48:31 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Pointless]]></category>
		<category><![CDATA[Attributes]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=930</guid>
		<description><![CDATA[Get ready for a roller coaster ride around the insanity that is me. You might actually find it amusing but most likely you&#8217;ll just leave sick or underwhelmed. Don&#8217;t feel bad if you do, you wouldn&#8217;t be the first and, thanks to somehow being impervious to Natural Selection, you won&#8217;t be the last. Be proud. [...]]]></description>
			<content:encoded><![CDATA[<p>Get ready for a roller coaster ride around the insanity that is me. You might actually find it amusing but most likely you&#8217;ll just leave sick or underwhelmed. Don&#8217;t feel bad if you do, you wouldn&#8217;t be the first and, thanks to somehow being impervious to Natural Selection, you won&#8217;t be the last. Be proud.</p>
<p>Here&#8217;s what this post is about: Say you have a url like eh:</p>
<pre>/User/View/1</pre>
<p>Where 1 represents a UserId to a user that exists in some matter of context. (Does that makes sense?) Well there are a couple of things that could go wrong here. For one, if you don&#8217;t want to have a nullable id in your signature:</p>
<pre>  <span style="color: #0000ff;">public</span> <span style="color: #008080;">ActionResult</span> View(<span style="color: #008080;">Int32</span>? userId)</pre>
<p>This could cause ouch:</p>
<pre>/User/View/</pre>
<p>But this problem is deeper. Much deeper. In fact so deep it deeper than even Piper Perabo has ever deeped before. Yrraaaahh!</p>
<p>What if the id doesn&#8217;t even refer to anything? Say the id is 101 but there&#8217;s no user with the id of 101? Beyond that, what if the id could be Id in some routes but UserId in others? WHAT WOULD YOU DO???</p>
<p>The idea here is to have something neato like this:</p>
<pre>  [<span style="color: #008080;">UserExistsById</span>]
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">ActionResult</span> View(<span style="color: #008080;">Int32</span> userId)</pre>
<p>And if that id is junk, then you redirect to an error page. Well if this sounds interesting, I&#8217;d be surprised, but read on in the event that it does.</p>
<p>Now this isn&#8217;t accomplished in the most simple manor, but for good reason: The more time in means the less time repeating. First method we need is something to simply check the route data to see if something exists:</p>
<pre>  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #008080;">Boolean</span> RouteDataValueExists(<span style="color: #008080;">ActionExecutingContext</span> filterContext, <span style="color: #008080;">String</span> idToCheck)
  {
    <span style="color: #0000ff;">return</span> filterContext.RouteData.Values.ContainsKey(idToCheck);
  }</pre>
<p>Real easy. It either has been digested by MVC and regurgitated into some kind of route value or it hasn&#8217;t. Basically this is the first check. After all, if it doesn&#8217;t exist why bother going further?</p>
<p>Next is the method that will be calling this one. Mainly one that uses the RouteDataValueExists and if returns true, then it actually checks the value against where ever the user is persisted.</p>
<pre>  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #008080;">Boolean</span> ValueFoundAndItemExists
  (
    <span style="color: #008080;">ActionExecutingContext</span> filterContext,
    <span style="color: #008080;">Enum</span> idToUse,
    <span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">ActionExecutingContext</span>, <span style="color: #008080;">Enum</span>, <span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">Int32</span>, <span style="color: #008080;">Boolean</span>&gt;, <span style="color: #008080;">Boolean</span>&gt; check,
    <span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">Int32</span>, <span style="color: #008080;">Boolean</span>&gt; exists
  )
  {
    <span style="color: #008080;">Boolean</span> checksOut = <span style="color: #0000ff;">false</span>;
    <span style="color: #008080;">String</span> convertedId = idToUse.ToString();

    <span style="color: #0000ff;">if</span> (RouteDataValueExists(filterContext, convertedId))
    {
      checksOut = check(filterContext, idToUse, exists);
    }

    <span style="color: #0000ff;">return</span> checksOut;
  }</pre>
<p>Ok so kind of a lot at first and it&#8217;s hard to decide how to present this system, so just go with it.</p>
<pre>  <span style="color: #008080;">Enum</span> idToUse,</pre>
<p>This is a design choice. In reality this will be turned into a string anyhow, but the idea is it will be what to check the route values for. So if you are checking for UserId, you will pass in UserRequestTypes.UserId. Again this was a choice on my part as I hate passing text around.</p>
<pre>  <span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">ActionExecutingContext</span>, <span style="color: #008080;">Enum</span>, <span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">Int32</span>, <span style="color: #008080;">Boolean</span>&gt;, <span style="color: #008080;">Boolean</span>&gt; check,</pre>
<p>This is the method that will be used by ValueFoundAndItemExists to delegate out the actual checking if the id is an integer and is a real user.</p>
<pre><span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">Int32</span>, <span style="color: #008080;">Boolean</span>&gt; exists</pre>
<p>This will be the method that you will use to delegate the whole checking if it exists in the database. Something like:</p>
<pre>  <span style="color: #0000ff;">class</span> <span style="color: #008080;">User</span>
  {
    <span style="color: #0000ff;">public</span> Exists(<span style="color: #008080;">Int32</span> id)
    {
       <span style="color: #0000ff;">return</span> EntityContext.Context.User.Any(user =&gt; user.Id == id);
    }
  }</pre>
<p>Still with me? No? Greeeeat. Next up is the base class that contains the</p>
<pre>  <span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">ActionExecutingContext</span>, <span style="color: #008080;">Enum</span>, <span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">Int32</span>, <span style="color: #008080;">Boolean</span>&gt;, <span style="color: #008080;">Boolean</span>&gt; check,</pre>
<p>parameter from above.</p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">abstract</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">BaseExistsAttribute</span> : <span style="color: #008080;">ActionFilterAttribute</span>
{
  <span style="color: #0000ff;">protected</span> <span style="color: #008080;">Boolean</span> Exists
  (
    <span style="color: #008080;">ActionExecutingContext</span> filterContext,
    <span style="color: #008080;">Enum</span> idToUse,
    <span style="color: #008080;">Func</span>&lt;<span style="color: #008080;">Int32</span>, <span style="color: #008080;">Boolean</span>&gt; exists
  )
  {
    <span style="color: #008080;">Int32</span>? id = <span style="color: #008080;">Convert</span>.ToString(filterContext.RouteData.Values[idToUse.ToString()]).ConvertTo&lt;<span style="color: #008080;">Int32</span>&gt;();
    <span style="color: #0000ff;">return</span> id.HasValue &amp;&amp; exists(id.Value);
  }
}</pre>
<p>Ok so something might look familiar like well basically all the parameters. The reason for this is that there is a little bit of delegate passing going on with this whole system. One method passes on methods to other method in some kind of crazy method hoe down without the flanel shirts.</p>
<p>You will notice the use of the enum in this part:</p>
<pre>  idToUse.ToString()</pre>
<p>Again, you can easily just pass in a string instead of an enum, I just did this to stay away from strings.</p>
<p>Only thing that might be of interest is the ToConvert method that you <a href="http://byatool.com/index.php/utilities/duck-typing-my-way-to-a-universal-string-convert/">can find here</a>. You don&#8217;t have to use it, you can simply just do a try parse on the</p>
<pre>  filterContext.RouteData.Values[idToUse.ToString()])</pre>
<p>To get an integer. That&#8217;s up to you. I could have just put that in the code for you to see but then I couldn&#8217;t randomly plug another post of mine.</p>
<p>Finally you have the actual attribute class:</p>
<pre>  [<span style="color: #008080;">AttributeUsage</span>(<span style="color: #008080;">AttributeTargets</span>.Method)]
  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">QuoteExistsByQuoteId</span> : <span style="color: #008080;">BaseExistsAttribute</span>
  {
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnActionExecuting(<span style="color: #008080;">ActionExecutingContext</span> filterContext)
    {
      base.OnActionExecuting(filterContext);

      <span style="color: #0000ff;">if</span>
      (
        !<span style="color: #008080;">MvcMethods</span>.ValueFoundAndItemExists
        (
          filterContext,
          <span style="color: #008080;">UserRequestTypes</span>.UserId,
          Exists,
          <span style="color: #008080;">UserEntity</span>.Exists
        )
      )
      {
        filterContext.Result = <span style="color: #008080;">SiteMethods</span>.ErrorOut(<span style="color: #008080;">ErrorNames</span>.General_PageError);
      }
    }
  }</pre>
<p>And now it all comes together.</p>
<pre>  <span style="color: #008080;">UserRequestTypes</span>.UserId</pre>
<p>This is the enum I&#8217;ve been talking about this entire post! Now that I think of it, not really exciting.</p>
<pre>  Exists</pre>
<p>That&#8217;s the method on the base class that checks for everything.</p>
<pre>  <span style="color: #008080;">UserEntity</span>.Exists</pre>
<p>And that&#8217;s the method I need to check the persistence if the user is real. Remember? Takes in an integer and returns a boolean? Yes? Yes? No?</p>
<p>What&#8217;s ErrorOut? Again this is just a method I made to create an ActionResult that is an redirect to an error page. Not hugely important. It&#8217;s just what handles the situation when the value is bogus&#8230;. dude.</p>
<pre>  [<span style="color: #008080;">UserExistsById</span>]
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">ActionResult</span> View(<span style="color: #008080;">Int32</span> userId)</pre>
<p>And there you have it. Hopefully it was useful in some way but I&#8217;m not of touch with reality.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/pointless/asp-net-mvc-attibute-to-check-if-route-value-exists-and-and-means-something/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC:  Attributes and Semi Dynamic Check for Request Parameters</title>
		<link>http://byatool.com/c/asp-net-mvc-attributes-and-semi-dynamic-check-for-request-parameters/</link>
		<comments>http://byatool.com/c/asp-net-mvc-attributes-and-semi-dynamic-check-for-request-parameters/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 00:46:59 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Attributes]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=803</guid>
		<description><![CDATA[If I were self involved I would say something silly like IN THIS AWESOME POST but I&#8217;m not. However in this post that is awesome I gave some examples of how to use attributes to set defaults or just check to see if an incoming id could be matched to a record somewhere&#8230; Sorry lost [...]]]></description>
			<content:encoded><![CDATA[<p>If I were self involved I would say something silly like <a href="http://byatool.com/index.php/lessons/asp-net-mvc-attributes-actionfilterattribute-and-why-you-might-want-to-use-them/">IN THIS AWESOME POST</a> but I&#8217;m not. However in <a href="http://byatool.com/index.php/lessons/asp-net-mvc-attributes-actionfilterattribute-and-why-you-might-want-to-use-them/">this post that is awesome</a> I gave some examples of how to use attributes to set defaults or just check to see if an incoming id could be matched to a record somewhere&#8230; Sorry lost my track of thought. Watching the begining of Transformers&#8230; You know the part where it could have been good.</p>
<p>Anyway, I figured I&#8217;d add another debatable use for an attribute, the CheckForGivenRequest one. Basically in the other post I had something that was specific it was checking for, this is used if you are checking for a request parameter but you don&#8217;t want to make an attribute for each and every one of them.</p>
<pre>  <span style="color: #008000;">//This is so you can use it many times on the same method</span>
  <span style="color: #008000;">//I know, I know... duh</span>
  [<span style="color: #008080;">AttributeUsage</span>(<span style="color: #008080;">AttributeTargets</span>.Method, AllowMultiple = <span style="color: #0000ff;">true</span>)]
  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">sealed</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">CheckForGivenRequestAttribute</span> : <span style="color: #008080;">ActionFilterAttribute</span>
  {
    <span style="color: #008000;">//The constructor to set what should be looked for
</span>    <span style="color: #008000;">//Default amount is what it should be set if not there</span>
    <span style="color: #0000ff;">public</span> <span style="color: #008080;">CheckForGivenRequestAttribute</span>(<span style="color: #008080;">String</span> requestParameterName, <span style="color: #008080;">Object</span> defaultAmount)
    {
      DefaultAmount = defaultAmount;
      RequestParameterName = requestParameterName;
    }

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnActionExecuting(ActionExecutingContext filterContext)
    {
      base.OnActionExecuting(filterContext);
      <span style="color: #008000;">//Check the extra request parameters (ie &amp;someId=1) for if it exists
</span>      <span style="color: #008000;">//If it doesn't exist, then add it</span>
      <span style="color: #0000ff;">if</span> (!filterContext.ActionParameters.ContainsKey(RequestParameterName))
      {
        filterContext.ActionParameters.Add(RequestParameterName, <span style="color: #0000ff;">null</span>);
      }

      <span style="color: #008000;">//If it's null set to the default
</span>      filterContext.ActionParameters[RequestParameterName] =
         filterContext.ActionParameters[RequestParameterName] ?? DefaultAmount;
    }

    <span style="color: #008000;">//Just the properties, nothing to see here.  Go away...</span>
    <span style="color: #0000ff;">private</span> Object DefaultAmount { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
    <span style="color: #0000ff;">private</span> String RequestParameterName { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span>; }
  }</pre>
<p>And then the use of it:</p>
<pre>  [<span style="color: #008080;">CheckForGivenRequestAttribute</span>(<span style="color: #800000;">"someStupidId"</span>, 1)]
  <span style="color: #0000ff;">public</span> <span style="color: #008080;">ActionResult</span> INeedAnExample(<span style="color: #008080;">Int32</span> someStupidId)
  {
    ...
  }</pre>
<p>Now you might ask why not make that attiribute generic to avoid boxing.</p>
<p><strong>Why not make that attribute generic to avoid boxing?</strong></p>
<p>Glad you asked. Turns out that you can&#8217;t make attributes generic. Aparrently <a href="http://stackoverflow.com/questions/294216/why-does-c-forbid-generic-attribute-types">it&#8217;s somwhat debatable why</a> but not possible at the time being. Besides, the ActionParameters collection is &lt;<span style="color: #008080;">String</span>, <span style="color: #008080;">Object</span>&gt; anyhow, so at some point any stuct would be boxed anyhow.</p>
<p>On a side note, I never noticed this before, but when one of the non descript Autobots crashes near a pool, some kid is there to ask if he is the Tooth Fairy?  Seriously?  Are kids really that dumb?  Cause every picture I&#8217;ve seen of the Tooth Fairy has been a 20 foot tall metal thing with no discernible features.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/c/asp-net-mvc-attributes-and-semi-dynamic-check-for-request-parameters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net MVC: Attributes, ActionFilterAttribute, and Why You Might Want To Use Them</title>
		<link>http://byatool.com/lessons/asp-net-mvc-attributes-actionfilterattribute-and-why-you-might-want-to-use-them/</link>
		<comments>http://byatool.com/lessons/asp-net-mvc-attributes-actionfilterattribute-and-why-you-might-want-to-use-them/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 21:49:23 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Attributes]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=793</guid>
		<description><![CDATA[So if you&#8217;ve used MVC, and I know you have because you want to be cool like me, you&#8217;ve most likely run into an error like this: Certain parameter wasn&#8217;t found. Make [parameter] nullable. In other words, the parameter didn&#8217;t show up in the URL and therefor MVC can&#8217;t assign a value to it. This [...]]]></description>
			<content:encoded><![CDATA[<p>So if you&#8217;ve used MVC, and I know you have because you want to be cool like me, you&#8217;ve most likely run into an error like this:</p>
<blockquote><p>Certain parameter wasn&#8217;t found. Make [parameter] nullable.</p></blockquote>
<p>In other words, the parameter didn&#8217;t show up in the URL and therefor MVC can&#8217;t assign a value to it. This would of course happen with anything that isn&#8217;t nullable. Now you could do this:</p>
<pre>  <span style="color: #0000ff;">void</span> SomeAction(<span style="color: #008080;">Int32</span>? pageNumber)
  {
    <span style="color: #0000ff;">if</span>(someParameter.HasValue)
    {
      ..
    }
  }</pre>
<p>But do you really feel like putting that in every stupid method that happens to have the same parameter? Say the parameter is something simple like pageNumber. Taking the above code, you would have to see if pageNumber has a value and if it doesn&#8217;t, continue to set some variable to a default. Kind of annoying to have to repeat that over and over again when you know you will be looking for pageNumber on many actions. Well Attributes, or more importantly ActionFilterAttribute, are the way to get around this.</p>
<p>Say with the pageNumber example you want a default of 1 if it doesn&#8217;t exist. Well that&#8217;s easy to do, it would be something like this:</p>
<pre>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">sealed</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">CheckPageNumberAttribute</span> : <span style="color: #008080;">ActionFilterAttribute</span>
    {
        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">const</span> <span style="color: #008080;">String</span> DefaultPageNumber = 0;

        <span style="color: #008000;">//This will fire automatically on page load.
</span>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnActionExecuting(<span style="color: #008080;">ActionExecutingContext</span> filterContext)
        {
            base.OnActionExecuting(filterContext);
            <span style="color: #008000;">//ActionParameters.Values holds all the request parameters after the ? as in ?pageNumber=1</span>
            <span style="color: #008000;">//See if it exists first.  If not, add it</span>
            <span style="color: #0000ff;">if</span> (!filterContext.ActionParameters.ContainsKey(<span style="color: #800000;">"pageNumber"</span>))
            {
                filterContext.ActionParameters.Add(RequestConstants.PageNumber, <span style="color: #0000ff;">null</span>);
            }

            <span style="color: #008000;">//Set to default if it's not null</span>
            filterContext.ActionParameters[<span style="color: #800000;">"pageNumber"</span>] = filterContext.ActionParameters[<span style="color: #800000;">"pageNumber"</span>] ?? DefaultPageNumber;
        }
    }</pre>
<p>So the method looks more like this now:</p>
<pre>  [<span style="color: #008080;">CheckPageNumberAttribute</span>]
  <span style="color: #0000ff;">void</span> SomeAction(<span style="color: #008080;">Int32</span> someParameter)
  {
    ...
  }</pre>
<p>And you can now do your &#8230; without worry.</p>
<p>But what if the url is like this?</p>
<blockquote><p>ShowMeACoolRoom/Index/1/</p></blockquote>
<p>And you have your route set up like:</p>
<pre>  <span style="color: #800000;">{controller}/{action}/{roomId}</span></pre>
<p>And you want to make sure that not only does that userId exist in the url, but the user actually exists. Well you don&#8217;t look to the ActionParameters.</p>
<pre>  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">sealed</span> <span style="color: #0000ff;">class</span> <span style="color: #008080;">RoomExistsAttribute</span> : <span style="color: #008080;">ActionFilterAttribute</span>
  {
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> OnActionExecuting(<span style="color: #008080;">ActionExecutingContext</span> context)
    {
      base.OnActionExecuting(context);

      <span style="color: #008080;">ChatRoom</span> foundRoom = <span style="color: #0000ff;">null</span>;
      <span style="color: #008000;">//RouteData.Values holds all the values in the url it was able to match to a route</span>
      <span style="color: #008000;">//Check the Values list in the RouteData for the id
</span>      <span style="color: #0000ff;">if</span>(context.RouteData.Values.ContainsKey(<span style="color: #800000;">"id"</span>))
      {
        <span style="color: #008000;">//Convert to an integer, ConvertTo <a href="http://byatool.com/index.php/utilities/duck-typing-my-way-to-a-universal-string-convert/">is just a method I've made</a>.</span>
        <span style="color: #008080;">Int32</span>? roomId =
          Convert.ToString(context.RouteData.Values[<span style="color: #800000;">"id"</span>]).ConvertTo();
        <span style="color: #0000ff;">if</span>(roomId.HasValue)
        {
            foundRoom = ChatRoom.GetRoomByID(roomId.Value);
        }
     }

     <span style="color: #008000;">//If the room isn't found, handle the error.</span>
     <span style="color: #0000ff;">if</span> (foundRoom == <span style="color: #0000ff;">null</span>)
     {
       <span style="color: #008000;">//redirect on error...</span>
       context.Result =
         <span style="color: #0000ff;">new</span> <span style="color: #008080;">RedirectToRouteResult</span>
         (
           GeneralConstants.RouteName,
           <span style="color: #0000ff;">new</span> <span style="color: #008080;">RouteValueDictionary</span>
           {
              { <span style="color: #800000;">"controller"</span>, <span style="color: #800000;">"sharedError"</span> },
              {  <span style="color: #800000;">"action"</span>, <span style="color: #800000;">"error"</span> },
              { <span style="color: #800000;">"name"</span>, <span style="color: #800000;">"someErrorKey"</span> }
            }
          );
       }
    }
  }</pre>
<p>Now the new method looks something like this:</p>
<pre>  [<span style="color: #008080;">RoomExistsAttribute</span>]
  [<span style="color: #008080;">CheckPageNumberAttribute</span>]
  <span style="color: #0000ff;">void</span> SomeAction(<span style="color: #008080;">Int32</span> roomId, <span style="color: #008080;">Int32</span> someParameter)
  {
    ...
  }</pre>
<p>And once again you are free to &#8230; without worry of the room not existing. Next post I plan on adding some more &#8220;advanced&#8221; attributes&#8230; hahaha advanced? This site? hahahaha sorry. But I will be adding a new post about certain attribute solutions I&#8217;ve found that work well.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/asp-net-mvc-attributes-actionfilterattribute-and-why-you-might-want-to-use-them/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

