本文整理汇总了C#中IHttpContext.SetStatusToBadRequest方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpContext.SetStatusToBadRequest方法的具体用法?C# IHttpContext.SetStatusToBadRequest怎么用?C# IHttpContext.SetStatusToBadRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpContext
的用法示例。
在下文中一共展示了IHttpContext.SetStatusToBadRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandlePut
private void HandlePut(IHttpContext context, string transformer)
{
var data = context.ReadJsonObject<TransformerDefinition>();
if (data == null || string.IsNullOrEmpty(data.TransformResults))
{
context.SetStatusToBadRequest();
context.Write("Expected json document with 'TransformResults' property");
return;
}
try
{
var transformerName = Database.PutTransform(transformer, data);
context.SetStatusToCreated("/transformers");
context.WriteJson(new {Transformer = transformerName});
}
catch (Exception ex)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Message = ex.Message,
Error = ex.ToString()
});
}
}
示例2: Respond
public override void Respond(IHttpContext context)
{
var src = context.Request.QueryString["from"];
var dbid = context.Request.QueryString["dbid"];
if (dbid == Database.TransactionalStorage.Id.ToString())
throw new InvalidOperationException("Both source and target databases have database id = " + dbid + "\r\nDatabase cannot replicate to itself.");
if (string.IsNullOrEmpty(src))
{
context.SetStatusToBadRequest();
return;
}
while (src.EndsWith("/"))
src = src.Substring(0, src.Length - 1);// remove last /, because that has special meaning for Raven
if (string.IsNullOrEmpty(src))
{
context.SetStatusToBadRequest();
return;
}
switch (context.Request.HttpMethod)
{
case "GET":
OnGet(context, src);
break;
case "PUT":
OnPut(context, src);
break;
}
}
示例3: Respond
public override void Respond(IHttpContext context)
{
var src = context.Request.QueryString["from"];
if (string.IsNullOrEmpty(src))
{
context.SetStatusToBadRequest();
return;
}
while (src.EndsWith("/"))
src = src.Substring(0, src.Length - 1);// remove last /, because that has special meaning for Raven
if (string.IsNullOrEmpty(src))
{
context.SetStatusToBadRequest();
return;
}
using (ReplicationContext.Enter())
{
var document = Database.Get(ReplicationConstants.RavenReplicationSourcesBasePath + "/" + src, null);
if (document == null)
{
log.DebugFormat("Got replication last etag request from {0}: [{1}]", src, new SourceReplicationInformation());
context.WriteJson(new SourceReplicationInformation());
}
else
{
var sourceReplicationInformation = document.DataAsJson.JsonDeserialization<SourceReplicationInformation>();
log.DebugFormat("Got replication last etag request from {0}: [{1}]", src, sourceReplicationInformation);
context.WriteJson(sourceReplicationInformation);
}
}
}
示例4: Respond
public override void Respond(IHttpContext context)
{
var auth = context.Request.Headers["Authorization"];
if(string.IsNullOrEmpty(auth))
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Could not find authorization header"
});
return;
}
if (auth.StartsWith("Bearer") == false)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Authorization header does not starts with Bearer"
});
return;
}
context.Response.SetCookie("OAuth-Token", Uri.EscapeDataString(auth));
}
示例5: OnGet
private void OnGet(IHttpContext context)
{
int port;
if (!int.TryParse(context.Request.QueryString["port"], out port))
{
context.SetStatusToBadRequest();
return;
}
string action = context.Request.QueryString["action"];
if (string.IsNullOrEmpty(action))
{
context.SetStatusToBadRequest();
return;
}
if (!servers.ContainsKey(port))
{
return;
}
var server = servers[port];
switch (action.ToLowerInvariant())
{
case "waitforallrequeststocomplete":
SpinWait.SpinUntil(() => server.Server.HasPendingRequests == false, TimeSpan.FromMinutes(15));
break;
default:
context.SetStatusToBadRequest();
return;
}
}
示例6: Respond
public override void Respond(IHttpContext context)
{
var src = context.Request.QueryString["from"];
if (string.IsNullOrEmpty(src))
{
context.SetStatusToBadRequest();
return;
}
while (src.EndsWith("/"))
src = src.Substring(0, src.Length - 1);// remove last /, because that has special meaning for Raven
if (string.IsNullOrEmpty(src))
{
context.SetStatusToBadRequest();
return;
}
var array = context.ReadJsonArray();
if (ReplicationTask != null)
ReplicationTask.HandleHeartbeat(src);
using (Database.DisableAllTriggersForCurrentThread())
{
Database.TransactionalStorage.Batch(actions =>
{
string lastEtag = Guid.Empty.ToString();
foreach (RavenJObject document in array)
{
var metadata = document.Value<RavenJObject>("@metadata");
if (metadata[Constants.RavenReplicationSource] == null)
{
// not sure why, old document from when the user didn't have replciation
// that we suddenly decided to replicate, choose the source for that
metadata[Constants.RavenReplicationSource] = RavenJToken.FromObject(src);
}
lastEtag = metadata.Value<string>("@etag");
var id = metadata.Value<string>("@id");
document.Remove("@metadata");
ReplicateDocument(actions, id, metadata, document, src);
}
var replicationDocKey = Constants.RavenReplicationSourcesBasePath + "/" + src;
var replicationDocument = Database.Get(replicationDocKey, null);
var lastAttachmentId = Guid.Empty;
if (replicationDocument != null)
{
lastAttachmentId =
replicationDocument.DataAsJson.JsonDeserialization<SourceReplicationInformation>().
LastAttachmentEtag;
}
Database.Put(replicationDocKey, null,
RavenJObject.FromObject(new SourceReplicationInformation
{
Source = src,
LastDocumentEtag = new Guid(lastEtag),
LastAttachmentEtag = lastAttachmentId,
ServerInstanceId = Database.TransactionalStorage.Id
}),
new RavenJObject(), null);
});
}
}
示例7: Respond
public override void Respond(IHttpContext context)
{
var src = context.Request.QueryString["from"];
if (string.IsNullOrEmpty(src))
{
context.SetStatusToBadRequest();
return;
}
while (src.EndsWith("/"))
src = src.Substring(0, src.Length - 1);// remove last /, because that has special meaning for Raven
if (string.IsNullOrEmpty(src))
{
context.SetStatusToBadRequest();
return;
}
var array = context.ReadBsonArray();
using (ReplicationContext.Enter())
{
Database.TransactionalStorage.Batch(actions =>
{
byte[] lastEtag = Guid.Empty.ToByteArray();
foreach (JObject attachment in array)
{
var metadata = attachment.Value<JObject>("@metadata");
if(metadata[ReplicationConstants.RavenReplicationSource] == null)
{
// not sure why, old attachment from when the user didn't have replciation
// that we suddenly decided to replicate, choose the source for that
metadata[ReplicationConstants.RavenReplicationSource] = JToken.FromObject(src);
}
lastEtag = attachment.Value<byte[]>("@etag");
var id = attachment.Value<string>("@id");
ReplicateAttachment(actions, id, metadata, attachment.Value<byte[]>("data"), new Guid(lastEtag), src);
}
var replicationDocKey = ReplicationConstants.RavenReplicationSourcesBasePath + "/" + src;
var replicationDocument = Database.Get(replicationDocKey,null);
var lastDocId = Guid.Empty;
if(replicationDocument != null)
{
lastDocId =
replicationDocument.DataAsJson.JsonDeserialization<SourceReplicationInformation>().
LastDocumentEtag;
}
Database.Put(replicationDocKey, null,
JObject.FromObject(new SourceReplicationInformation
{
LastDocumentEtag = lastDocId,
LastAttachmentEtag = new Guid(lastEtag)
}),
new JObject(), null);
});
}
}
示例8: Respond
public override void Respond(IHttpContext context)
{
var target = LogManager.GetTarget<DatabaseMemoryTarget>();
if(target == null)
{
context.SetStatusToNotFound();
context.WriteJson(new
{
Error = "DatabaseMemoryTarget was not registered in the log manager, logs endpoint disabled"
});
return;
}
var database = Database;
if(database == null)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "No database found."
});
return;
}
if(database.Name == null)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Database name is null."
});
return;
}
IEnumerable<LogEventInfo> log = target[Database.Name].GeneralLog;
switch (context.Request.QueryString["type"])
{
case "error":
case "warn":
log = target[Database.Name].WarnLog;
break;
}
context.WriteJson(log.Select(x => new
{
x.TimeStamp,
Message = x.FormattedMessage,
x.LoggerName,
Level = x.Level.ToString(),
Exception = x.Exception == null ? null : x.Exception.ToString()
}));
}
示例9: RespondToAdmin
public override void RespondToAdmin(IHttpContext context)
{
var docEtagStr = context.Request.QueryString["docEtag"];
Guid docEtag;
var attachmentEtagStr = context.Request.QueryString["attachmentEtag"];
Guid attachmentEtag;
if (Guid.TryParse(docEtagStr, out docEtag) == false & // intentionally so, we want to eval both sides
Guid.TryParse(attachmentEtagStr, out attachmentEtag) == false)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "The query string variable 'docEtag' or 'attachmentEtag' must be set to a valid guid"
});
return;
}
Database.TransactionalStorage.Batch(accessor =>
{
if(docEtag != Guid.Empty)
{
accessor.Lists.RemoveAllBefore(Constants.RavenReplicationDocsTombstones, docEtag);
}
if(attachmentEtag != Guid.Empty)
{
accessor.Lists.RemoveAllBefore(Constants.RavenReplicationAttachmentsTombstones, attachmentEtag);
}
});
}
示例10: OnBulkOperation
private void OnBulkOperation(IHttpContext context, Func<string, IndexQuery, bool, RavenJArray> batchOperation)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var index = match.Groups[2].Value;
if (string.IsNullOrEmpty(index))
{
context.SetStatusToBadRequest();
return;
}
var allowStale = context.GetAllowStale();
var indexQuery = context.GetIndexQueryFromHttpContext(maxPageSize: int.MaxValue);
var status = new BulkOperationStatus();
var sp = Stopwatch.StartNew();
long id = 0;
var task = Task.Factory.StartNew(() =>
{
var array = batchOperation(index, indexQuery, allowStale);
status.State = array;
status.Completed = true;
context.Log(log => log.Debug("\tBatch Operation worked on {0:#,#;;0} documents in {1}, task #: {2}", array.Length, sp.Elapsed, id));
});
Database.AddTask(task, status, out id);
context.WriteJson(new
{
OperationId = id
});
}
示例11: Respond
public override void Respond(IHttpContext context)
{
if (context.User.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can look at the server stats"
});
return;
}
if(ResourceStore != DefaultResourceStore)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Admin stats can only be had from the root database"
});
return;
}
context.WriteJson(server.Statistics);
}
示例12: RespondToAdmin
public override void RespondToAdmin(IHttpContext context)
{
if (EnsureSystemDatabase(context) == false)
return;
var db = context.Request.QueryString["database"];
if(string.IsNullOrWhiteSpace(db))
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Compact request requires a valid database parameter"
});
return;
}
var configuration = server.CreateTenantConfiguration(db);
if (configuration == null)
{
context.SetStatusToNotFound();
context.WriteJson(new
{
Error = "No database named: " + db
});
return;
}
server.LockDatabase(db, () =>
SystemDatabase.TransactionalStorage.Compact(configuration));
}
示例13: Respond
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var userId = match.Groups[1].Value;
var docIds = context.Request.QueryString.GetValues("id");
var operation = context.Request.QueryString["operation"];
var transactionInformation = GetRequestTransaction(context);
if (docIds == null || string.IsNullOrEmpty(operation) || string.IsNullOrEmpty(userId))
{
context.SetStatusToBadRequest();
return;
}
// we don't want security to take hold when we are trying to ask about security
using (Database.DisableAllTriggersForCurrentThread())
{
var documents = docIds.Select(docId=>Database.GetDocumentMetadata(docId, transactionInformation)).ToArray();
var etag = CalculateEtag(documents, userId);
if (context.MatchEtag(etag))
{
context.SetStatusToNotModified();
return;
}
context.Response.AddHeader("ETag", etag.ToString());
context.WriteJson(GenerateAuthorizationResponse(documents, docIds, operation, userId));
}
}
示例14: Respond
public override void Respond(IHttpContext context)
{
if (context.User.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can initiate a database compact procedure"
});
return;
}
if(DefaultResourceStore != ResourceStore)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Compact request can only be issued from the system database"
});
return;
}
var db = context.Request.QueryString["database"];
if(string.IsNullOrWhiteSpace(db))
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Compact request requires a valid database parameter"
});
return;
}
var configuration = server.CreateTenantConfiguration(db);
if (configuration == null)
{
context.SetStatusToNotFound();
context.WriteJson(new
{
Error = "No database named: " + db
});
return;
}
server.LockDatabase(db, () =>
DefaultResourceStore.TransactionalStorage.Compact(configuration));
}
示例15: Respond
/// <summary>
/// Responds the specified context.
/// </summary>
/// <param name="context">The context.</param>
public override void Respond(IHttpContext context)
{
var match = urlMatcher.Match(context.GetRequestUrl());
var index = match.Groups[1].Value;
var indexEtag = Database.GetIndexEtag(index, null);
if (context.MatchEtag(indexEtag))
{
context.SetStatusToNotModified();
return;
}
var term = context.Request.QueryString["term"];
var field = context.Request.QueryString["field"];
StringDistanceTypes distanceTypes;
int numOfSuggestions;
float accuracy;
if (Enum.TryParse(context.Request.QueryString["distance"], true, out distanceTypes) == false)
distanceTypes = StringDistanceTypes.Default;
if (distanceTypes == StringDistanceTypes.None)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Suggestion is disabled since you specified the Distance value as 'StringDistanceTypes.None'."
});
return;
}
if (int.TryParse(context.Request.QueryString["max"], out numOfSuggestions) == false)
numOfSuggestions = 10;
if (float.TryParse(context.Request.QueryString["accuracy"], out accuracy) == false)
accuracy = 0.5f;
bool popularity;
if (bool.TryParse(context.Request.QueryString["popularity"], out popularity) == false)
popularity = false;
var query = new SuggestionQuery
{
Distance = distanceTypes,
Field = field,
MaxSuggestions = numOfSuggestions,
Term = term,
Accuracy = accuracy,
Popularity = popularity
};
var suggestionQueryResult = Database.ExecuteSuggestionQuery(index, query);
context.WriteETag(Database.GetIndexEtag(index, null));
context.WriteJson(suggestionQueryResult);
}