Added WithCookie extension for response.

This commit is contained in:
MattMo 2022-02-06 22:33:55 -08:00
parent 41bbabf1cb
commit b7837f6639
2 changed files with 22 additions and 5 deletions

View File

@ -19,7 +19,7 @@ namespace MontoyaTech.Rest.Net
/// </summary> /// </summary>
/// <param name="response"></param> /// <param name="response"></param>
/// <param name="text"></param> /// <param name="text"></param>
/// <returns></returns> /// <returns>This response.</returns>
public static HttpListenerResponse WithText(this HttpListenerResponse response, string text) public static HttpListenerResponse WithText(this HttpListenerResponse response, string text)
{ {
response.ContentType = "text/plain; charset=utf-16"; response.ContentType = "text/plain; charset=utf-16";
@ -35,7 +35,7 @@ namespace MontoyaTech.Rest.Net
/// </summary> /// </summary>
/// <param name="response"></param> /// <param name="response"></param>
/// <param name="obj"></param> /// <param name="obj"></param>
/// <returns></returns> /// <returns>This response.</returns>
public static HttpListenerResponse WithJson(this HttpListenerResponse response, object obj) public static HttpListenerResponse WithJson(this HttpListenerResponse response, object obj)
{ {
response.ContentType = "application/json; charset=utf-16"; response.ContentType = "application/json; charset=utf-16";
@ -51,7 +51,7 @@ namespace MontoyaTech.Rest.Net
/// </summary> /// </summary>
/// <param name="response"></param> /// <param name="response"></param>
/// <param name="filePath"></param> /// <param name="filePath"></param>
/// <returns></returns> /// <returns>This response.</returns>
public static HttpListenerResponse WithFile(this HttpListenerResponse response, string filePath) public static HttpListenerResponse WithFile(this HttpListenerResponse response, string filePath)
{ {
response.ContentType = "application/octet-stream"; response.ContentType = "application/octet-stream";
@ -70,7 +70,7 @@ namespace MontoyaTech.Rest.Net
/// </summary> /// </summary>
/// <param name="response"></param> /// <param name="response"></param>
/// <param name="status"></param> /// <param name="status"></param>
/// <returns></returns> /// <returns>This response.</returns>
public static HttpListenerResponse WithStatus(this HttpListenerResponse response, HttpStatusCode status) public static HttpListenerResponse WithStatus(this HttpListenerResponse response, HttpStatusCode status)
{ {
try try
@ -83,5 +83,22 @@ namespace MontoyaTech.Rest.Net
return response; return response;
} }
} }
/// <summary>
/// Sets a cookie for a given response.
/// </summary>
/// <param name="response"></param>
/// <param name="name">The name of the cookie</param>
/// <param name="value">The value of the cookie</param>
/// <returns>This response.</returns>
public static HttpListenerResponse WithCookie(this HttpListenerResponse response, string name, string value)
{
if (response.Cookies[name] == null)
response.Cookies.Add(new Cookie(name, value));
else
response.Cookies[name].Value = value;
return response;
}
} }
} }

View File

@ -14,7 +14,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName> <AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace> <RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile> <GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.0.1</Version> <Version>1.0.2</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json"> <PackageReference Include="Newtonsoft.Json">