当前位置: 首页>>代码示例>>C#>>正文


C# Negotiation.Negotiator类代码示例

本文整理汇总了C#中Nancy.Responses.Negotiation.Negotiator的典型用法代码示例。如果您正苦于以下问题:C# Negotiator类的具体用法?C# Negotiator怎么用?C# Negotiator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Negotiator类属于Nancy.Responses.Negotiation命名空间,在下文中一共展示了Negotiator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Should_add_negotiated_headers_to_response

        public void Should_add_negotiated_headers_to_response()
        {
            // Given

            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/headers", x =>
                {
                    var context =
                        new NancyContext { NegotiationContext = new NegotiationContext() };

                    var negotiator =
                        new Negotiator(context);
                    negotiator.WithHeader("foo", "bar");

                    return negotiator;
                });
            });

            var brower = new Browser(with =>
            {
                with.ResponseProcessor<TestProcessor>();

                with.Module(module);
            });

            // When
            var response = brower.Get("/headers");

            // Then
            Assert.True(response.Headers.ContainsKey("foo"));
            Assert.Equal("bar", response.Headers["foo"]);
        }
开发者ID:uxTomas,项目名称:Nancy,代码行数:33,代码来源:ContentNegotiationFixture.cs

示例2: Should_add_negotiated_content_headers_to_response

        public void Should_add_negotiated_content_headers_to_response()
        {
            // Given

              var module = new ConfigurableNancyModule(with =>
              {
            with.Get("/headers", (x, m) =>
            {
              var context =
                  new NancyContext { NegotiationContext = new NegotiationContext() };

              var negotiator =
                  new Negotiator(context);
              negotiator.WithContentType("text/xml");

              return negotiator;
            });
              });

              var brower = new Browser(with =>
              {
            with.ResponseProcessor<TestProcessor>();

            with.Module(module);
              });

              // When
              var response = brower.Get("/headers");

              // Then
              Assert.Equal("text/xml", response.Context.Response.ContentType);
        }
开发者ID:garethrhughes,项目名称:Nancy,代码行数:32,代码来源:ContentNegotiationFixture.cs

示例3: Should_apply_default_accept_when_no_accept_header_sent

        public void Should_apply_default_accept_when_no_accept_header_sent()
        {
            // Given
            var browser = new Browser(with =>
            {
                with.ResponseProcessor<TestProcessor>();

                with.Module(new ConfigurableNancyModule(x =>
                {
                    x.Get("/", parameters =>
                    {
                        var context =
                            new NancyContext { NegotiationContext = new NegotiationContext() };

                        var negotiator =
                            new Negotiator(context);

                        return negotiator;
                    });
                }));
            });

            // When
            var response = browser.Get("/");

            // Then
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
开发者ID:uxTomas,项目名称:Nancy,代码行数:28,代码来源:ContentNegotiationFixture.cs

示例4: AddNegotiatedHeaders

 private static void AddNegotiatedHeaders(Negotiator negotiator, Response response)
 {
     foreach (var header in negotiator.NegotiationContext.Headers)
     {
         response.Headers[header.Key] = header.Value;
     }
 }
开发者ID:JelleHissink,项目名称:Nancy,代码行数:7,代码来源:DefaultRouteInvoker.cs

示例5: CreateNegotiatedResponse

        private static Response CreateNegotiatedResponse(NancyContext context, IResponseNegotiator responseNegotiator, Exception exception)
        {
            HttpServiceError httpServiceError = HttpServiceErrorUtilities.ExtractFromException(exception, HttpServiceErrorDefinition.GeneralError);

            Negotiator negotiator = new Negotiator(context)
                .WithHttpServiceError(httpServiceError);

            return responseNegotiator.NegotiateResponse(negotiator, context);
        }
开发者ID:alana1,项目名称:NancySamples,代码行数:9,代码来源:CustomErrorHandler.cs

示例6: Handle

        public void Handle(HttpStatusCode statusCode, NancyContext context)
        {
            context.NegotiationContext = new NegotiationContext();

            Negotiator negotiator = new Negotiator(context)
                .WithHttpServiceError(HttpServiceErrorDefinition.InternalServerError);

            context.Response = responseNegotiator.NegotiateResponse(negotiator, context);
        }
开发者ID:alana1,项目名称:NancySamples,代码行数:9,代码来源:StatusCodeHandler500.cs

示例7: CreateNegotiatedResponse

        private static Response CreateNegotiatedResponse(NancyContext context, IResponseNegotiator responseNegotiator, Exception exception, HttpServiceError defaultError)
        {
            HttpServiceError httpServiceError = ExtractFromException(exception, defaultError);

            Negotiator negotiator = new Negotiator(context)
                .WithServiceError(httpServiceError);

            return responseNegotiator.NegotiateResponse(negotiator, context);
        }
开发者ID:bytefish,项目名称:GcmSample,代码行数:9,代码来源:CustomErrorHandler.cs

示例8: Handle

        public void Handle(HttpStatusCode statusCode, NancyContext context)
        {
            context.NegotiationContext = new NegotiationContext();

            Negotiator negotiator = new Negotiator(context)
                .WithServiceError(new NotFoundError(context.Request.Path));

            context.Response = responseNegotiator.NegotiateResponse(negotiator, context);
        }
开发者ID:bytefish,项目名称:GcmSample,代码行数:9,代码来源:StatusCodeHandler404.cs

示例9: Handle

        public void Handle(HttpStatusCode statusCode, NancyContext context)
        {
            context.NegotiationContext = new NegotiationContext();

            Negotiator negotiator = new Negotiator(context)
                .WithStatusCode(HttpServiceErrorDefinition.NotFoundError.HttpStatusCode)
                .WithModel(HttpServiceErrorDefinition.NotFoundError.ServiceErrorModel);

            context.Response = responseNegotiator.NegotiateResponse(negotiator, context);
        }
开发者ID:alana1,项目名称:NancySamples,代码行数:10,代码来源:StatusCodeHandler404.cs

示例10: DefaultError

        private Response DefaultError(NancyContext ctx)
        {
            var defaultErrorRepresentation = new ErrorRepresentation(HttpStatusCode.InternalServerError, DefaultErrorCode);

            defaultErrorRepresentation.Message = DefaultErrorMessage;

            var negotiator = new Negotiator(ctx).WithStatusCode(HttpStatusCode.InternalServerError).WithModel(defaultErrorRepresentation);

            return responseNegotiator.NegotiateResponse(negotiator, ctx);
        }
开发者ID:onatm,项目名称:futurice.challenge,代码行数:10,代码来源:ErrorHandlingPipelines.cs

示例11: Error

        private Response Error(Exception rootException, NancyContext ctx)
        {
            var errorMapping = errorMap[rootException.GetType()];

            var errorRepresentation = errorMapping.Representation;

            if (string.IsNullOrEmpty(errorRepresentation.Message))
            {
                errorRepresentation.Message = rootException.Message;
            }

            var negotiator = new Negotiator(ctx).WithStatusCode(errorMapping.HttpStatusCode).WithModel(errorRepresentation);

            return responseNegotiator.NegotiateResponse(negotiator, ctx);
        }
开发者ID:onatm,项目名称:futurice.challenge,代码行数:15,代码来源:ErrorHandlingPipelines.cs

示例12: HandleException

        public static Response HandleException(NancyContext ctx, Exception ex, IResponseNegotiator responder)
        {
            LogException(ctx, ex);

            // Return AjaxError JSON if the client was making an AJAX request. Otherwise show an error page.
            var negotiator = new Negotiator(ctx);
            negotiator = negotiator.WithStatusCode(HttpStatusCode.InternalServerError)
                // Use this model as a JSON response if the client asked for application/json
                .WithMediaRangeModel("application/json", new AjaxError(AjaxError.InternalError, "Sorry, something went wrong when processing your request."))

                // Use this model as the model for the view if anything other than JSON
                .WithModel(new ErrorViewModel(ex))
                // Use this view for text/html
                .WithView("Error");
            return responder.NegotiateResponse(negotiator, ctx);
        }
开发者ID:mageomageos,项目名称:animerecs,代码行数:16,代码来源:ErrorHandler.cs

示例13: Handle

        public void Handle(HttpStatusCode statusCode, NancyContext context)
        {
            var response = new Negotiator(context);

            Error error = null;
            if (context.Items.ContainsKey("OnErrorException"))
            {
                var exception = context.Items["OnErrorException"] as Exception;
                error = new Error { ErrorMessage = exception.Message, FullException = exception.ToString() };
            }

            response.WithModel(new ErrorPageViewModel
                {
                    Title = "Sorry, something went wrong",
                    Summary = error == null ? "An unexpected error occurred." : error.ErrorMessage,
                    Details = error == null ? null : error.FullException // TODO: Obey "ShowFullErrors" flag in config
                }).WithStatusCode(statusCode).WithView("Error");

            var errorresponse = responseNegotiator.NegotiateResponse(response, context);
            string s = "";
            context.Response = errorresponse;
        }
开发者ID:jchannon,项目名称:NowinReproIssue,代码行数:22,代码来源:ErrorStatusCodeHandler.cs

示例14: Should_set_reason_phrase_on_response

        public void Should_set_reason_phrase_on_response()
        {
            // Given
            var module = new ConfigurableNancyModule(with =>
            {
                with.Get("/customPhrase", (x, m) =>
                {
                    var context =
                        new NancyContext();

                    var negotiator =
                        new Negotiator(context);
                    negotiator.WithReasonPhrase("The test is passing!").WithStatusCode(404);

                    return negotiator;
                });
            });

            var browser = new Browser(with =>
            {
                with.StatusCodeHandler<DefaultStatusCodeHandler>();
                with.ResponseProcessor<TestProcessor>();
                with.Module(module);
            });

            // When
            var response = browser.Get("/customPhrase", with => with.Accept("application/json"));

            // Then
            Assert.Equal("The test is passing!", response.ReasonPhrase);
        }
开发者ID:dwonisch,项目名称:Nancy,代码行数:31,代码来源:ContentNegotiationFixture.cs

示例15: GetCompatibleHeaders

        private Tuple<string, IEnumerable<Tuple<IResponseProcessor, ProcessorMatch>>>[] GetCompatibleHeaders(NancyContext context, Negotiator negotiator)
        {
            var coercedAcceptHeaders = this.GetCoercedAcceptHeaders(context).ToArray();

            List<Tuple<string, decimal>> acceptHeaders;

            var permissableMediaRanges = negotiator.NegotiationContext.PermissableMediaRanges;

            if (permissableMediaRanges.Any(mr => mr.IsWildcard))
            {
                acceptHeaders = coercedAcceptHeaders
                    .Where(header => header.Item2 > 0m)
                    .ToList();
            }
            else
            {
                acceptHeaders = coercedAcceptHeaders.Where(header => header.Item2 > 0m)
                    .SelectMany(header => permissableMediaRanges.Where(mr => mr.Matches(header.Item1)).Select(mr => Tuple.Create(mr.ToString(), header.Item2)))
                    .ToList();
            }

            return (from header in acceptHeaders
                    let compatibleProcessors = (IEnumerable<Tuple<IResponseProcessor, ProcessorMatch>>)GetCompatibleProcessorsByHeader(header.Item1, negotiator.NegotiationContext.GetModelForMediaRange(header.Item1), context)
                    where compatibleProcessors != null
                    select new Tuple<string, IEnumerable<Tuple<IResponseProcessor, ProcessorMatch>>>(
                        header.Item1,
                        compatibleProcessors
                    )).ToArray();
        }
开发者ID:JelleHissink,项目名称:Nancy,代码行数:29,代码来源:DefaultRouteInvoker.cs


注:本文中的Nancy.Responses.Negotiation.Negotiator类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。