本文整理汇总了C#中HttpContext.EmitStringMessage方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContext.EmitStringMessage方法的具体用法?C# HttpContext.EmitStringMessage怎么用?C# HttpContext.EmitStringMessage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpContext
的用法示例。
在下文中一共展示了HttpContext.EmitStringMessage方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindParameter
internal static object[] BindParameter(HttpContext httpContext, ILightNodeOptions options,IOperationCoordinator coordinator, ValueProvider valueProvider, ParameterInfoSlim[] arguments)
{
var methodParameters = new object[arguments.Length];
for (int i = 0; i < arguments.Length; i++)
{
var item = arguments[i];
var _values = valueProvider.GetValue(item.Name);
var value = _values as string;
var values = _values as List<string>;
var isEmpty = _values == null;
if (isEmpty && !item.ParameterTypeIsArray)
{
if (item.IsOptional)
{
methodParameters[i] = item.DefaultValue;
continue;
}
else if ((!item.ParameterTypeIsString || options.ParameterStringImplicitNullAsDefault) && (item.ParameterTypeIsClass || item.ParameterTypeIsNullable))
{
methodParameters[i] = null;
continue;
}
else
{
coordinator.OnProcessInterrupt(options, httpContext, InterruptReason.ParameterBindMissing, "Lack of Parameter:" + item.Name);
options.Logger.ParameterBindMissing(OperationMissingKind.LackOfParameter, item.Name);
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ThrowException)
{
throw new ParameterMissingException(OperationMissingKind.LackOfParameter, item.Name);
}
else
{
httpContext.EmitBadRequest();
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ReturnErrorStatusCodeIncludeErrorDetails)
{
httpContext.EmitStringMessage("Lack of Parameter:" + item.Name);
}
return null;
}
}
}
else if (!item.ParameterTypeIsArray)
{
var conv = TypeBinder.GetConverter(item.ParameterType, !options.ParameterEnumAllowsFieldNameParse);
if (conv == null) throw new InvalidOperationException("critical:register code is broken");
object pValue;
if (conv(value ?? values[0], out pValue))
{
methodParameters[i] = pValue;
continue;
}
else if (item.IsOptional)
{
methodParameters[i] = item.DefaultValue;
continue;
}
else if ((!item.ParameterTypeIsString || options.ParameterStringImplicitNullAsDefault) && (item.ParameterTypeIsClass || item.ParameterTypeIsNullable))
{
methodParameters[i] = null;
continue;
}
else
{
coordinator.OnProcessInterrupt(options, httpContext, InterruptReason.ParameterBindMissing, "Mismatch ParameterType:" + item.Name);
options.Logger.ParameterBindMissing(OperationMissingKind.MissmatchParameterType, item.Name);
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ThrowException)
{
throw new ParameterMissingException(OperationMissingKind.MissmatchParameterType, item.Name);
}
else
{
httpContext.EmitBadRequest();
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ReturnErrorStatusCodeIncludeErrorDetails)
{
httpContext.EmitStringMessage("Mismatch ParameterType:" + item.Name);
}
return null;
}
}
}
var arrayConv = TypeBinder.GetArrayConverter(item.ParameterType, !options.ParameterEnumAllowsFieldNameParse);
if (arrayConv == null) throw new InvalidOperationException("critical:register code is broken");
methodParameters[i] = arrayConv((values != null) ? values : (value != null) ? new[] { value } : (IList<string>)new string[0]);
continue;
}
return methodParameters;
}
示例2: SelectHandler
OperationHandler SelectHandler(HttpContext httpContext, IOperationCoordinator coorinator, out AcceptVerbs verb, out string ext)
{
// out default
verb = AcceptVerbs.Get;
ext = "";
var path = httpContext.Request.Path.Value;
var method = httpContext.Request.Method;
// extract path
var keyBase = path.Trim('/').Split('/');
if (keyBase.Length != 2)
{
goto NOT_FOUND;
}
// extract "extension" for media type
var extStart = keyBase[1].LastIndexOf(".");
if (extStart != -1)
{
ext = keyBase[1].Substring(extStart + 1);
keyBase[1] = keyBase[1].Substring(0, keyBase[1].Length - ext.Length - 1);
}
// {ClassName, MethodName}
var key = new RequestPath(keyBase[0], keyBase[1]);
OperationHandler handler;
if (handlers.TryGetValue(key, out handler))
{
// verb check
if (StringComparer.OrdinalIgnoreCase.Equals(method, "GET"))
{
verb = AcceptVerbs.Get;
}
else if (StringComparer.OrdinalIgnoreCase.Equals(method, "POST"))
{
verb = AcceptVerbs.Post;
}
else if (StringComparer.OrdinalIgnoreCase.Equals(method, "PUT"))
{
verb = AcceptVerbs.Put;
}
else if (StringComparer.OrdinalIgnoreCase.Equals(method, "DELETE"))
{
verb = AcceptVerbs.Delete;
}
else if (StringComparer.OrdinalIgnoreCase.Equals(method, "PATCH"))
{
verb = AcceptVerbs.Patch;
}
else
{
goto VERB_MISSING;
}
if (!handler.AcceptVerb.HasFlag(verb))
{
goto VERB_MISSING;
}
return handler; // OK
}
else
{
goto NOT_FOUND;
}
VERB_MISSING:
coorinator.OnProcessInterrupt(options, httpContext, InterruptReason.MethodNotAllowed, "MethodName:" + method);
options.Logger.MethodNotAllowed(OperationMissingKind.MethodNotAllowed, path, method);
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ThrowException)
{
throw new MethodNotAllowedException(OperationMissingKind.MethodNotAllowed, path, method);
}
else
{
httpContext.EmitMethodNotAllowed();
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ReturnErrorStatusCodeIncludeErrorDetails)
{
httpContext.EmitStringMessage("MethodNotAllowed:" + method);
}
return null;
}
NOT_FOUND:
coorinator.OnProcessInterrupt(options, httpContext, InterruptReason.OperationNotFound, "SearchedPath:" + path);
options.Logger.OperationNotFound(OperationMissingKind.OperationNotFound, path);
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ThrowException)
{
throw new OperationNotFoundException(OperationMissingKind.MethodNotAllowed, path);
}
else
{
httpContext.EmitNotFound();
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ReturnErrorStatusCodeIncludeErrorDetails)
{
httpContext.EmitStringMessage("OperationNotFound:" + path);
}
return null;
}
//.........这里部分代码省略.........
示例3: NegotiateFormat
// Accept, Accept-Encoding flow
internal IContentFormatter NegotiateFormat(HttpContext httpContext, string ext, ILightNodeOptions options, IOperationCoordinator coorinator)
{
var requestHeader = httpContext.Request.Headers;
StringValues accepts;
if (ForceUseFormatter != null) return ForceUseFormatter;
if (!string.IsNullOrWhiteSpace(ext))
{
// Ext match -> ContentEncoding match
var selectedFormatters = formatterByExt[ext] as ICollection<IContentFormatter> ?? formatterByExt[ext].ToArray();
if (!selectedFormatters.Any())
{
coorinator.OnProcessInterrupt(options, httpContext, InterruptReason.NegotiateFormatFailed, "Ext:" + ext);
options.Logger.NegotiateFormatFailed(OperationMissingKind.NegotiateFormatFailed, ext);
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ThrowException)
{
throw new NegotiateFormatFailedException(OperationMissingKind.NegotiateFormatFailed, ext);
}
else
{
httpContext.EmitNotAcceptable();
if (options.OperationMissingHandlingPolicy == OperationMissingHandlingPolicy.ReturnErrorStatusCodeIncludeErrorDetails)
{
httpContext.EmitStringMessage("NegotiateFormat failed, ext:" + ext);
}
}
return null;
}
return SelectAcceptEncodingFormatter(requestHeader, selectedFormatters);
}
else if (requestHeader.TryGetValue("Accept", out accepts))
{
if (optionFormatters.Length == 1) return options.DefaultFormatter; // optimize path, defaultFormatter only
// MediaType match -> ContentEncoding match
var acceptsValues = GetDescendingQualityHeaderValues(accepts);
var formatters = acceptsValues.SelectMany(x => formatterByMediaType[x.Item3]).ToArray();
if (formatters.Length == 0)
{
// only accept-encoding
goto CONTENT_ENCODING_MATCH;
}
return SelectAcceptEncodingFormatter(requestHeader, formatters);
}
// ContentEncoding match
CONTENT_ENCODING_MATCH:
{
if (optionFormatters.Length == 1) return options.DefaultFormatter; // optimize path, defaultFormatter only
// ContentEncoding match
StringValues rawAcceptEncoding;
if (!requestHeader.TryGetValue("Accept-Encoding", out rawAcceptEncoding))
{
return options.DefaultFormatter;
}
var acceptEncodings = GetDescendingQualityHeaderValues(rawAcceptEncoding);
var formatter = acceptEncodings
.Select(kvp => formatterByContentEncoding[kvp.Item3].FirstOrDefault())
.FirstOrDefault(x => x != null);
if (formatter == null) return options.DefaultFormatter;
return formatter;
}
}
示例4: IsRethrowOrEmitException
static bool IsRethrowOrEmitException(IOperationCoordinator coordinator, ILightNodeOptions options, HttpContext httpContext, Exception ex)
{
var exString = ex.ToString();
coordinator.OnProcessInterrupt(options, httpContext, InterruptReason.ExecuteFailed, exString);
switch (options.ErrorHandlingPolicy)
{
case ErrorHandlingPolicy.ReturnInternalServerError:
httpContext.EmitInternalServerError();
httpContext.EmitStringMessage("500 InternalServerError");
return false;
case ErrorHandlingPolicy.ReturnInternalServerErrorIncludeErrorDetails:
httpContext.EmitInternalServerError();
httpContext.EmitStringMessage(exString);
return false;
case ErrorHandlingPolicy.ThrowException:
default:
httpContext.EmitInternalServerError();
return true;
}
}