snipt

Ctrl+h for KB shortcuts

C#

C# controllers for WebApi using Action Names and Verbs

1
2
3
4
5
6
7
[ValidateUser, ActionName("subjects"), HttpPost]
public HttpResponseMessage PostSubject(SubjectDto item)
{
	// 	This means if the controller was called say member, you could then
    //	call this function by api/member/subjects and the verb HttpPost or 
	//	whatever defines the type of action being performed
}
https://snipt.net/embed/44999a792692b2d0be01830da5ac1904/
/raw/44999a792692b2d0be01830da5ac1904/
44999a792692b2d0be01830da5ac1904
csharp
C#
7
2019-08-15T15:51:42
True
False
False
Jan 06, 2013 at 09:01 AM
/api/public/snipt/52809/
c-controllers-for-webapi-using-action-names-and-verbs
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="na">[ValidateUser, ActionName(&quot;subjects&quot;), HttpPost]</span> </span><span id="L-2"><a name="L-2"></a><span class="k">public</span> <span class="n">HttpResponseMessage</span> <span class="nf">PostSubject</span><span class="p">(</span><span class="n">SubjectDto</span> <span class="n">item</span><span class="p">)</span> </span><span id="L-3"><a name="L-3"></a><span class="p">{</span> </span><span id="L-4"><a name="L-4"></a> <span class="c1">// This means if the controller was called say member, you could then</span> </span><span id="L-5"><a name="L-5"></a> <span class="c1">// call this function by api/member/subjects and the verb HttpPost or </span> </span><span id="L-6"><a name="L-6"></a> <span class="c1">// whatever defines the type of action being performed</span> </span><span id="L-7"><a name="L-7"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
c#, rest, webapi

C#

Return the current method/Module/Page name in c#

1
2
3
//	Useful for error trapping etc to return what method failed etc..

this.GetType().FullName
https://snipt.net/embed/198f3c4d26821443017cd98198277f88/
/raw/198f3c4d26821443017cd98198277f88/
198f3c4d26821443017cd98198277f88
csharp
C#
3
2019-08-19T05:12:04
True
False
False
/api/public/snipt/48577/
return-the-current-methodmodulepage-name-in-c
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// Useful for error trapping etc to return what method failed etc..</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="k">this</span><span class="p">.</span><span class="n">GetType</span><span class="p">().</span><span class="n">FullName</span> </span></pre></div> </td></tr></table>
.net, c#

C#

c# / SQL reading varbinary as a stream, or converting to text

1
2
3
4
5
6
7
8
MemoryStream m = new MemoryStream(byteArrayFromDB);
StreamReader file = new StreamReader(m);

// If you need to read the stream out as text/string..
while ((line = file.ReadLine()) != null)
{
string code = line.Substring(line.Length - 12);
}
https://snipt.net/embed/a385f8d8f772cdf16e5e697302f1d7ec/
/raw/a385f8d8f772cdf16e5e697302f1d7ec/
a385f8d8f772cdf16e5e697302f1d7ec
csharp
C#
8
2019-08-18T12:03:03
True
False
False
/api/public/snipt/43355/
c-sql-reading-varbinary-as-a-stream-or-converting-to-text
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">MemoryStream</span> <span class="n">m</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MemoryStream</span><span class="p">(</span><span class="n">byteArrayFromDB</span><span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="n">StreamReader</span> <span class="n">file</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StreamReader</span><span class="p">(</span><span class="n">m</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="c1">// If you need to read the stream out as text/string..</span> </span><span id="L-5"><a name="L-5"></a><span class="k">while</span> <span class="p">((</span><span class="n">line</span> <span class="p">=</span> <span class="n">file</span><span class="p">.</span><span class="n">ReadLine</span><span class="p">())</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span> </span><span id="L-6"><a name="L-6"></a><span class="p">{</span> </span><span id="L-7"><a name="L-7"></a><span class="kt">string</span> <span class="n">code</span> <span class="p">=</span> <span class="n">line</span><span class="p">.</span><span class="n">Substring</span><span class="p">(</span><span class="n">line</span><span class="p">.</span><span class="n">Length</span> <span class="p">-</span> <span class="m">12</span><span class="p">);</span> </span><span id="L-8"><a name="L-8"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
binary, c#, sql, stream

C#

JSON.NET: Deserialize to an object

1
2
3
4
5
6
7
8
// Taken from a blogpost on Scott Hanselman's blog.. we grab some JSON,
// and using the JSON.NET JObject, we can parse the response/JSON to a dynamic object 'o'

var client = new WebClient();
client.Headers.Add("User-Agent", "Nobody");
var response = client.DownloadString(new Uri("http://www.hanselman.com/smallestdotnet/json.ashx"));
JObject o = JObject.Parse(response);'
//Now o is an object I can walk around...
https://snipt.net/embed/5979a8fac8ef45989adffe927b4ebb4f/
/raw/5979a8fac8ef45989adffe927b4ebb4f/
5979a8fac8ef45989adffe927b4ebb4f
csharp
C#
8
2019-08-19T01:12:38
True
False
False
/api/public/snipt/41569/
jsonnet-deserialize-to-an-object
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// Taken from a blogpost on Scott Hanselman&#39;s blog.. we grab some JSON,</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// and using the JSON.NET JObject, we can parse the response/JSON to a dynamic object &#39;o&#39;</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="kt">var</span> <span class="n">client</span> <span class="p">=</span> <span class="k">new</span> <span class="n">WebClient</span><span class="p">();</span> </span><span id="L-5"><a name="L-5"></a><span class="n">client</span><span class="p">.</span><span class="n">Headers</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="s">&quot;User-Agent&quot;</span><span class="p">,</span> <span class="s">&quot;Nobody&quot;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a><span class="kt">var</span> <span class="n">response</span> <span class="p">=</span> <span class="n">client</span><span class="p">.</span><span class="n">DownloadString</span><span class="p">(</span><span class="k">new</span> <span class="n">Uri</span><span class="p">(</span><span class="s">&quot;http://www.hanselman.com/smallestdotnet/json.ashx&quot;</span><span class="p">));</span> </span><span id="L-7"><a name="L-7"></a><span class="n">JObject</span> <span class="n">o</span> <span class="p">=</span> <span class="n">JObject</span><span class="p">.</span><span class="n">Parse</span><span class="p">(</span><span class="n">response</span><span class="p">);</span><span class="err">&#39;</span> </span><span id="L-8"><a name="L-8"></a><span class="c1">//Now o is an object I can walk around...</span> </span></pre></div> </td></tr></table>
c#, deserialize, json, json.net

C#

JSON.NET: Serialize an object to a JSON string

1
2
3
4
5
6
7
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
 
string json = JsonConvert.SerializeObject(product);
https://snipt.net/embed/d2f963e05479322d52c7bb5ae1e26af6/
/raw/d2f963e05479322d52c7bb5ae1e26af6/
d2f963e05479322d52c7bb5ae1e26af6
csharp
C#
7
2019-08-18T01:49:27
True
False
False
/api/public/snipt/41568/
jsonnet-serialize-an-object
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">Product</span> <span class="n">product</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Product</span><span class="p">();</span> </span><span id="L-2"><a name="L-2"></a><span class="n">product</span><span class="p">.</span><span class="n">Name</span> <span class="p">=</span> <span class="s">&quot;Apple&quot;</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="n">product</span><span class="p">.</span><span class="n">Expiry</span> <span class="p">=</span> <span class="k">new</span> <span class="n">DateTime</span><span class="p">(</span><span class="m">2008</span><span class="p">,</span> <span class="m">12</span><span class="p">,</span> <span class="m">28</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a><span class="n">product</span><span class="p">.</span><span class="n">Price</span> <span class="p">=</span> <span class="m">3.99</span><span class="n">M</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="n">product</span><span class="p">.</span><span class="n">Sizes</span> <span class="p">=</span> <span class="k">new</span> <span class="kt">string</span><span class="p">[]</span> <span class="p">{</span> <span class="s">&quot;Small&quot;</span><span class="p">,</span> <span class="s">&quot;Medium&quot;</span><span class="p">,</span> <span class="s">&quot;Large&quot;</span> <span class="p">};</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a><span class="kt">string</span> <span class="n">json</span> <span class="p">=</span> <span class="n">JsonConvert</span><span class="p">.</span><span class="n">SerializeObject</span><span class="p">(</span><span class="n">product</span><span class="p">);</span> </span></pre></div> </td></tr></table>
c#, json, json.net