简体   繁体   中英

NancyFX Error: Unable to locate view 'Int32' How do I fix this?

I'm learning NancyFX and I have a simple route that returns an id that a user requests. Here's the code:

using Nancy;

namespace NancyFXTutorial
{
    public class CarModule : NancyModule
    {
        public CarModule()
        {
            Get["/status"] = _ => "Hello World";

            Get["/car/{id}"] = parameters =>
            {
                int id = parameters.id;

                return Negotiate
                    .WithStatusCode(HttpStatusCode.OK)
                    .WithModel(id);
            };
        }
    }
}

When I request http://localhost/car/43234 , I get an error message that says: Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'Int32' Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'Int32'

What does this mean? How do I fix this?

There is an extension method you can chain onto Negotiate called WithView which lets you specify a view to use when the client requests an HTML response:

return Negotiate
                .WithStatusCode(HttpStatusCode.OK)
                .WithModel(id)
                .WithView("YourView");

The view you indicate can use the model you pass into WithModel .

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