简体   繁体   中英

Nancy routing for ASP.NET webforms

I've been trying to implement Nancy on my webforms project. I've read this guide: https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-asp.net

I've added this to my config:

<system.webServer>
    <handlers>
        <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="nancy/*" />
    </handler>
</system.webServer>

I've created a '/nancy' folder with a web.config file contaning:

<?xml version="1.0"?>
<configuration>
    <system.web>
        <httpHandlers>
        <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/>
     </httpHandlers>
    </system.web>
</configuration>

I have the following C# code:

public class TestApi : Nancy.NancyModule
{
    public TestApi()
        : base("/nancy")
    {
        Get["/ok"] = parameters =>
                            {
                                return "Ok";
                            };
    }
}

This works when accessing '/nancy/ok' But when I change my 'Get["/ok"]' to 'Get["/ok/ok"]' and access '/nancy/ok/ok' I get and 404 Not Found (the little troll image and all)

EDIT* If I leave it at 'Get["/ok"]' and access /ok/ok/ok I get "Ok" back...

Any ideas why I cant make a more specific route?

Greetings Mads

I just tried this in a test and my route worked without issue. Here's my test:

[TestFixture]
public class ScratchNancy
{
    [Test]
    public void RootTest()
    {
        var defaultNancyBootstrapper = new DefaultNancyBootstrapper();
        var browser = new Browser(defaultNancyBootstrapper);

        var result = browser.Get("/nancy/ok/ok", with => with.HttpRequest());

        Assert.AreEqual("It works!", result.Body.AsString());
    }

    public class RootModule : NancyModule
    {
        public RootModule() : base("/nancy")
        {
            Get["/ok/ok"] = p =>
                {
                    return Response.AsText("It works!");
                };
        }            
    }
}

The link that @TheCodeJunkie provided is a great resource for setting up Nancy for success. Here it is again: https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-asp.net

Did you remember to add this in your root web.config?

  <system.web>
    <httpHandlers>
      <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="nancy/*" />
    </httpHandlers>
  </system.web>

and this section in your nancy's folder web.config?

<httpHandlers>
  <add path="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" verb="*"/>
</httpHandlers>

If your problem persists, here's somewhat not elegant solution:

  • If you want /nancy/ok/ok , add another line to your webconfig:

     <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="nancy/*/*"> 
  • If you want /nancy/ok/ok/ok , add another line:

     <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="nancy/*/*/*"> 

Judging by your routes you want to run Nancy under the url /nancy , so any url under that is handled by Nancy while anything else defined on the root will continue to work under ASP.Net Web Forms.

If this is the case, the problem is you need to wrap your handler in a <location> tag:

<location path="nancy">
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpHandlers>
      <add verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/>
    </httpHandlers>
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <add name="Nancy" verb="*" type="Nancy.Hosting.Aspnet.NancyHttpRequestHandler" path="*"/>
    </handlers>
  </system.webServer>
</location>

This allows all requests (defined by path="*" ) that call under the location /nancy to be processed by Nancy.

This information can be found in the link you linked to under Adding Nancy to an existing site

https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-asp.net#adding-nancy-to-an-existing-site

^ Direct link to specific heading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM