本文整理汇总了C#中Nancy.Response类的典型用法代码示例。如果您正苦于以下问题:C# Response类的具体用法?C# Response怎么用?C# Response使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Response类属于Nancy命名空间,在下文中一共展示了Response类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeleteBucket
private Response DeleteBucket(string bucket)
{
storage.DeleteBucket(bucket);
var response = new Response { StatusCode = HttpStatusCode.NoContent };
return response;
}
示例2: AddObject
private Response AddObject(string bucket, string key, Stream stream)
{
if (Request.Url.Query == "?acl")
{
return new Response { StatusCode = HttpStatusCode.OK };
}
var content = stream.Copy(configuration.MaxBytesPerSecond);
var s3Object = new S3Object
{
Bucket = bucket,
Key = key,
ContentType = Request.Headers.ContentType,
CreationDate = DateTime.UtcNow,
Content = () => content,
ContentMD5 = content.GenerateMD5CheckSum(),
Size = content.Length
};
storage.AddObject(s3Object);
var response = new Response { StatusCode = HttpStatusCode.OK };
response.WithHeader("ETag", string.Format("\"{0}\"", s3Object.ContentMD5));
return response;
}
示例3: RequiresClaims
/// <summary>
/// Gets a request hook for checking claims
/// </summary>
/// <param name="claims">Required claims</param>
/// <returns>Before hook delegate</returns>
public static Func<NancyContext, Response> RequiresClaims(IEnumerable<string> claims)
{
return (ctx) =>
{
var failResponse = new Response() { StatusCode = HttpStatusCode.Forbidden };
object userClaimsObject;
if (!ctx.Items.TryGetValue(CLAIMS_KEY, out userClaimsObject))
{
return failResponse;
}
var userClaims = userClaimsObject as IEnumerable<string>;
if (userClaims == null)
{
return failResponse;
}
if (claims.Any(claim => !userClaims.Contains(claim)))
{
return failResponse;
}
return null;
};
}
示例4: Should_set_a_cookie_with_name_and_value
public void Should_set_a_cookie_with_name_and_value()
{
var response = new Response();
response.AddCookie("itsover", "9000");
response.Cookies.Count.ShouldEqual(1);
response.Cookies[0].ShouldEqual("itsover", "9000", null, null, null);
}
示例5: AsNancyResponse
public static Response AsNancyResponse(this OutgoingWebResponse openauthResponse)
{
var response = new Response();
var statusCode = HttpStatusCode.InternalServerError;
string statusCodeName = null;
if (openauthResponse.Status == System.Net.HttpStatusCode.Redirect)
{
statusCodeName = HttpStatusCode.TemporaryRedirect.ToString();
}
else
{
statusCodeName = openauthResponse.Status.ToString();
}
if (!Enum.TryParse<HttpStatusCode>(statusCodeName, out statusCode))
{
throw new Exception("Unknown Status Code: " + openauthResponse.Status.ToString());
}
response.StatusCode = statusCode;
var headers = new Dictionary<string, string>();
foreach(var key in openauthResponse.Headers.AllKeys)
{
headers.Add(key, openauthResponse.Headers[key]);
}
response.Headers = headers;
var body = Encoding.UTF8.GetBytes(openauthResponse.Body);
response.Contents = stream => stream.Write(body, 0, body.Length);
return response;
}
示例6: CompressResponse
private Response CompressResponse(Request request, Response response)
{
if (!response.ContentType.Contains("image")
&& request.Headers.AcceptEncoding.Any(x => x.Contains("gzip"))
&& (!response.Headers.ContainsKey("Content-Encoding") || response.Headers["Content-Encoding"] != "gzip"))
{
var data = new MemoryStream();
response.Contents.Invoke(data);
data.Position = 0;
if (data.Length < 1024)
{
response.Contents = stream =>
{
data.CopyTo(stream);
stream.Flush();
};
}
else
{
response.Headers["Content-Encoding"] = "gzip";
response.Contents = s =>
{
var gzip = new GZipStream(s, CompressionMode.Compress, true);
data.CopyTo(gzip);
gzip.Close();
};
}
}
return response;
}
示例7: ExportToCSV
public static Response ExportToCSV()
{
var calls = SupportCall.ToList();
var response = new Response();
response.Headers.Add("Content-Disposition", "attachment; filename=ExportedData" + DateTime.Now.ToString("dd MMM yyyy HH mm") + ".csv");
response.Contents = stream =>
{
StreamWriter sw = new StreamWriter(stream);
sw.Write("Internal Reference, Customer, Customer Reference, Customer Site, Customer Site Location, Assigned To, Product, Status, Created By, CreatedTime, Note Type, Note Sub Type, Note Text, Time Spent, Created By, Created Date Time");
sw.Write(sw.NewLine);
foreach (var call in calls)
{
var notes = Note.LoadFromCall(call.RecordID);
foreach (var note in notes.OrderByDescending(x => x.CreatedDateTime).ThenByDescending(x => x.NoteType))
{
sw.Write(call.InternalReference);
sw.Write(",");
sw.Write(call.CustomerName);
sw.Write(",");
sw.Write(call.CustomerReference);
sw.Write(",");
sw.Write(call.CustomerSiteName);
sw.Write(",");
sw.Write(call.CustomerSiteLocationName);
sw.Write(",");
sw.Write(call.AssignedTo);
sw.Write(",");
sw.Write(call.ProductName);
sw.Write(",");
sw.Write(call.Status);
sw.Write(",");
sw.Write(call.CreatedBy);
sw.Write(",");
sw.Write(call.CreatedDateTime);
sw.Write(",");
sw.Write(note.NoteType);
sw.Write(",");
sw.Write(note.NoteSubType);
sw.Write(",");
sw.Write(note.NoteText);
sw.Write(",");
sw.Write(note.TimeSpent);
sw.Write(",");
sw.Write(note.CreatedBy);
sw.Write(",");
sw.Write(note.CreatedDateTime);
sw.Write(sw.NewLine);
}
}
sw.Close();
};
return response;
}
示例8: CreateDinner
private Response CreateDinner()
{
var dinner = this.Bind<Dinner>();
nerdDinners.Dinners.Add(dinner);
try
{
nerdDinners.SaveChanges();
}
catch (DbEntityValidationException ex)
{
var message = ex.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage;
var response = new Response
{
StatusCode = HttpStatusCode.BadRequest,
Contents = stream =>
{
using (var writer = new StreamWriter(stream) { AutoFlush = true })
{
writer.WriteLine(message);
}
}
};
return response;
}
return new Response();
}
示例9: AuthenticateModule
public AuthenticateModule()
: base("/api")
{
Post["/AuthenticateUser"] = parameters =>
{
var bind = this.Bind<LoginRequest>();
//do something with request.Username and request.Password.
var response = new Response
{
StatusCode = HttpStatusCode.OK
};
response.AddCookie("valid", bind.Username, DateTime.Now.AddMinutes(5));
return response;
};
Get["/LogOff"] = parameters =>
{
var response = new Response
{
StatusCode = HttpStatusCode.OK
};
//clear the cookie
response.AddCookie("valid", null, DateTime.Now.AddYears(-5));
return response;
};
}
示例10: _initResponse
// Kept seperate, as I guess more CSVs will be needed
private static Response _initResponse()
{
Response response = new Response();
response.Headers.Add("Content-Disposition", "attachment; filename=ExportedDate" + DateTime.Now.ToString("dd MMM yyyy HH mm") + ".csv");
response.ContentType = "application/octet-stream";
return response;
}
示例11: HandleExceptions
private static Response HandleExceptions(Exception err, NancyContext ctx)
{
logger.Error("Failed {0} with {1}", ctx.Request.Path, err);
var result = new Response { ReasonPhrase = err.Message };
if (err is NotImplementedException)
{
result.StatusCode = HttpStatusCode.NotImplemented;
}
else if (err is UnauthorizedAccessException)
{
result.StatusCode = HttpStatusCode.Unauthorized;
}
else if (err is ArgumentException)
{
result.StatusCode = HttpStatusCode.BadRequest;
}
else
{
// An unexpected exception occurred!
result.StatusCode = HttpStatusCode.InternalServerError;
}
return result;
}
示例12: AuthModule
public AuthModule()
: base("/api")
{
Post["/authenticate"] = x => {
var bind = this.Bind<LoginRequest>();
UserManager<User> manager = new UserManager<User>(new UserStore());
var user = manager.Find(bind.Username, bind.Password);
if (user == null)
{
return new Response
{
StatusCode = HttpStatusCode.Unauthorized
};
}
else
{
var response = new Response
{
StatusCode = HttpStatusCode.OK
};
return response.WithCookie("sq-valid", user.UserName, DateTime.Now.AddMinutes(5));
}
};
Get["/logout"] = x => {
var response = new Response
{
StatusCode = HttpStatusCode.OK
};
return response.WithCookie("sq-valid", null, DateTime.Now.AddYears(-5));
};
}
示例13: AddBucket
private Response AddBucket(string bucketName)
{
var bucket = new Bucket { Id = bucketName, CreationDate = DateTime.UtcNow };
storage.AddBucket(bucket);
var response = new Response { StatusCode = HttpStatusCode.OK };
return response;
}
示例14: Should_set_a_cookie_with_name_and_value_and_expiry
public void Should_set_a_cookie_with_name_and_value_and_expiry()
{
var response = new Response();
var date = DateTime.Now;
response.AddCookie("itsover", "9000", date);
response.Cookies.Count.ShouldEqual(1);
response.Cookies[0].ShouldEqual("itsover", "9000", date, null, null);
}
示例15: Should_set_a_cookie_with_everything
public void Should_set_a_cookie_with_everything()
{
var response = new Response();
var date = DateTime.Now;
response.AddCookie("itsover", "9000", date, "life", "/andeverything");
response.Cookies.Count.ShouldEqual(1);
response.Cookies[0].ShouldEqual("itsover", "9000", date, "life", "/andeverything");
}