标签: nancy

NancyFX:反序列化JSON

文档建议NancyFx帮助我对json请求体进行WRT反序列化,但我不确定如何.请参阅以下测试以证明:

[TestFixture]
public class ScratchNancy
{
    [Test]
    public void RootTest()
    {
        var result = new Browser(new DefaultNancyBootstrapper()).Post(
            "/",
            with =>
                {
                    with.HttpRequest();
                    with.JsonBody(JsonConvert.SerializeObject(new DTO {Name = "Dto", Value = 9}));
                });

        Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
    }

    public class RootModule : NancyModule
    {
        public RootModule()
        {
            Post["/"] = Root;
        }

        private Response Root(dynamic o)
        {
            DTO dto = null;//how do I get the dto from the body of the request without reading the stream and deserializing myself?

            return HttpStatusCode.OK;
        }
    }

    public class …
Run Code Online (Sandbox Code Playgroud)

.net rest json nancy

7
推荐指数
1
解决办法
6761
查看次数

将MVC 2应用程序添加到IIS 7中的Nancy站点

在IIS 7中,我使用Nancy项目创建了一个网站.然后,我使用别名向站点添加了MVC 2应用程序api.我能够完美地访问南希项目中已定义的路线.但是,当我访问时/api,我收到以下错误:

Could not load type 'Nancy.Hosting.Aspnet.NancyHttpRequestHandler'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Could not load type 'Nancy.Hosting.Aspnet.NancyHttpRequestHandler'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the …
Run Code Online (Sandbox Code Playgroud)

iis asp.net-mvc iis-7 asp.net-mvc-2 nancy

6
推荐指数
1
解决办法
1500
查看次数

复制Razor-Views为自托管NancyFx?

我开始使用一个简单的MVC站点,使用NancyFx和Razor-views(.cshtml),并Nancy.Hosting.Aspnet使用IIS Express.现在我使用Nancy.Hosting.Self(和TopShelf)将项目改编为自托管服务.

但是,要提供视图,我似乎需要将其属性从None&更改Do not copyContent&Copy if newer,因此将它们复制到.\bin\Debug\.

一个结果是,在重新启动之前,不会显示/更新视图的更改.即使在调试模式下工作,默认情况下也禁用缓存.我知道这只是一个小麻烦,但仍然是一个烦恼,能够立即尝试(cs)HTML变化是很好的.所以我想知道有没有办法解决这个问题,例如为了防止必须将文件设置为Copy if newer

asp.net-mvc host razor nancy

6
推荐指数
1
解决办法
1056
查看次数

SignalR(具有自托管Nancy)显示404用于协商?clientProtocol = 1.3

我试图在控制台应用程序中运行Nancy中的SignalR.

当我的浏览器$.connection.hub.start()它得到404 - NotFound//localhost:667/negotiate?clientProtocol=1.3

---- 8 <----

我(尝试)在一个端口上运行Nancy而在另一个端口上运行SignalR.南希与剃刀合作.SignalR返回集线器javascript.

(对不起下面的代码量,但是我无法进一步减少它.)
(这个问题可能会从之前已被删除的问题中得到认可 - 我已经标记错误.)

客户代码:

    <script type="text/javascript" src='/Scripts/jquery-1.6.4.min.js'></script>
    <script type="text/javascript" src="/Scripts/jquery.signalR-2.0.0-beta2.js"></script>
    <script src="http://localhost:667/signalr/hubs" type="text/javascript"></script>
var chat;
$(function () {
    $.connection.hub.url = '//localhost:667';
    $.connection.hub.logging = true;
    chat = $.connection.chat;
    chat.client.addMessage = onAddMessage; // declared but not here

    $.connection.hub.start()
        .done(function () {
            alert($.connection.id);
            chat.server.send('Works!');
        })
        .fail(function ( failreason ) {
            alert( failreason );
        });
});
Run Code Online (Sandbox Code Playgroud)

服务器代码(在作为admin运行的控制台程序中)

class Program
{
    static void Main(string[] args)
    {
        const string webUrl = "http://localhost:666"; …
Run Code Online (Sandbox Code Playgroud)

.net self-hosting nancy signalr

6
推荐指数
1
解决办法
4170
查看次数

如何得到南希谈判员的回应?

我有一个NancyContext,我需要Response根据请求的正确内容协商者获得一个正文.我想我可以使用Nancy的Negotiator类来添加模型,设置状态和其他东西.但是,我需要返回一个子类型Response.那么,我可以使用什么来构建响应Negotiator

这是我的方法:

public Response ConvertToHttpResponse(Exception exception, NancyContext context)
{
    var negotiator = new Negotiator(context)
        .WithStatusCode(HttpStatusCode.BadRequest)
        .WithReasonPhrase(exception.Message);

    return ???;
}
Run Code Online (Sandbox Code Playgroud)

c# content-negotiation nancy

6
推荐指数
1
解决办法
8200
查看次数

NancyFx:将默认字符集设置为utf8

我在我的cshtml文件中有这个片段:

Expires on: @Model.EndDate.ToString("MMM dd yyyy")
Run Code Online (Sandbox Code Playgroud)

我在回复中得到了这个:

HTTP/1.1 200 OK
Content-Type: text/html

...
Expires on: ?????™ 05 2013
Run Code Online (Sandbox Code Playgroud)

如何告诉Nancy默认使用UTF8进行响应?


编辑:为了澄清,这不是一个本地化问题,输出已经本地化 - 只是本地化的UTF8字符串被发送到没有UTF8字符集声明的客户端,所以它被搞砸,试图将其视为latin1 .

我要找的是这个:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8

...
Run Code Online (Sandbox Code Playgroud)

而且我不想单独为每个响应指定它.

我正在使用NancyFx Web框架

c# nancy

6
推荐指数
1
解决办法
1935
查看次数

运行NancyFx时单声道未处理的异常

我正在运行Ubuntu 12.04,我安装了单一运行时包.在我的Windows机器上,我的NancyFx自托管应用程序编译好1警告,但当我尝试在我的VPS上运行它时,我收到以下错误:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or
assembly 'System.Core, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' or one
of its dependencies.
File name: 'System.Core, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load
file or assembly 'System.Core, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' or one of its dependencies.
File name: 'System.Core, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'
Run Code Online (Sandbox Code Playgroud)

关于如何解决这个问题的任何想法?

c# mono nancy ubuntu-12.04

6
推荐指数
2
解决办法
2825
查看次数

NancyFX + SSL-如何使“ this.RequireHttps()”在Linux上工作?

我的自托管NancyFX应用程序使用SSL,并且我使用“ this.RequiresHttps() ”将某些模块标记为“仅SSL”。在Windows上,我遵循了本教程:

https://github.com/NancyFx/Nancy/wiki/Accessing-the-client-certificate-when-using-SSL

后:

netsh http add sslcert ipport=0.0.0.0:1234 certhash=303b4adb5aeb17eeac00d8576693a908c01e0b71 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable
Run Code Online (Sandbox Code Playgroud)

我使用以下代码:

public static void Main(string[] args)
{
    List<Uri> uri2 = new List<Uri>();
    uri2.Add(new Uri("http://localhost:80"));
    uri2.Add(new Uri("https://localhost:1234"));

    HostConfiguration hc = new HostConfiguration()
    {
        EnableClientCertificates = true
    };

    using (var host = new NancyHost(hc,uri2.ToArray()))
    {
        host.Start();

        string runningOn = "\n\n";
        foreach(var item in uri2)
        {
            runningOn += item+"\n";
        }

        Console.WriteLine("Your application is running on " + runningOn/*uri2.First()*/);
        Console.WriteLine("Press any [Enter] to close the host.");
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

而且效果很好-可以在端口80上访问未加密的数据,而在端口1234上可以访问SSL。

问题是-我想在Linux主机上做同样的事情,但是似乎找不到与Windows“ …

c# linux mono nancy

6
推荐指数
1
解决办法
2165
查看次数

模型绑定到Nancy中的Dictionary <string,string>

我无法Dictionary<string,string>在Nancy中绑定JSON .

这条路线:

Get["testGet"] = _ =>
{
    var dictionary = new Dictionary<string, string>
    {
         {"hello", "world"},
         {"foo", "bar"}
    };

    return Response.AsJson(dictionary);
};
Run Code Online (Sandbox Code Playgroud)

按预期返回以下JSON:

{
    "hello": "world",
    "foo": "bar"
}
Run Code Online (Sandbox Code Playgroud)

当我尝试将这个确切的JSON发布回此路线时:

Post["testPost"] = _ =>
{
    var data = this.Bind<Dictionary<string, string>>();
    return null;
};
Run Code Online (Sandbox Code Playgroud)

我得到了例外:

值"[Hello,world]"不是"System.String"类型,不能在此通用集合中使用.

是否可以绑定到Dictionary<string,string>使用Nancys默认模型绑定,如果是这样,我在这里做错了什么?

c# model-binding nancy

6
推荐指数
1
解决办法
2452
查看次数

为什么Nancy不会自动将DefaultFluentAdapter注册到IFluentAdapterFactory?

这更像是"我为什么要这样做?" 而不是实际问题.

我决定按照NancyFX文档中的说明整合FluentValidation

NancyFX FluentValidation集成

文档说没有其他事情需要做.所以我从NuGet安装了这个包

Install-Package Nancy.Validation.FluentValidation -Version 1.4.1
Run Code Online (Sandbox Code Playgroud)

然后更新了Nancy.Validation.Fluentvalidation引用的根验证器

https://www.nuget.org/packages/FluentValidation/
Run Code Online (Sandbox Code Playgroud)

我创建了一个非常类似于docs的简单验证器来测试它.

并且在NancyModule中运行此句子时

this.Validate(user);
Run Code Online (Sandbox Code Playgroud)

我收到这样的错误

An exception of type 'Nancy.Validation.ModelValidationException

No model validator factory could be located. Please ensure that you have an appropriate validation package installed
Run Code Online (Sandbox Code Playgroud)

所以...在挖掘了一点之后我就告诉自己,好吧,如果找不到工厂,也许没有人(TinyIoC)正在解决我认为缺少的IFluentAdapterFactory依赖关系.所以我发现实际上有一个内置包的适配器工厂的实现,那就是DefaultFluentAdapterFactory

因此,在bootstraper中连接它可以解决它.

container.Register(typeof (IFluentAdapterFactory), typeof (DefaultFluentAdapterFactory));
Run Code Online (Sandbox Code Playgroud)

它应该被自动发现吗?或者我可能做错了,默认情况下不设置它?

提前致谢!

fluentvalidation nancy

6
推荐指数
0
解决办法
588
查看次数