WebApiContrib / WebApiContrib.Formatting.Razor

Web API formatter based on RazorViewEngine
MIT License
24 stars 10 forks source link

ArgumentException occured while view rendering #15

Open petrvmakarov opened 10 years ago

petrvmakarov commented 10 years ago

Models

public class CustomerDetials { public string FullName { get; set; } }

public class ServiceDetials { public string Name { get; set; } }

Controllers

//http://localhost/customer/details public class CustomerController : ApiController {
[HttpGet] public CustomerDetials Detials() { return new CustomerDetials { FullName = "Full Name" }; } }

//http://localhost/service/details public class ServiceController : ApiController {
[HttpGet] public ServiceDetials Detials() { return new ServiceDetials{ Name = "The service" }; } }

Views


CustomerDetials.cshtml:

@model Models.CustomerDetials @{ Layout = "~/Views/Shared/_Layout.cshtml"; }

Customer name is @Model.FullName


ServiceDetials.cshtml:

@model Models.ServiceDetials @{ Layout = "~/Views/Shared/_Layout.cshtml"; }

Service name is @Model.Name

Steps to reproduce: 1)browse to http://localhost/customer/details 1.1) Succefully 2)browse to http://localhost/service/details 2.1) Failed to render

ArgumentException occured in RazorViewParser::GetParsedView in return _templateService.Run(view.ViewName, view.Model, null);

Object of type 'Models.ServiceDetials' cannot be converted to type 'Models.CustomerDetials'

It looks like ceche issue. But I found a workaround if I remove Layout = "~/Views/Shared/_Layout.cshtml"; line from every view I would not get that issue.

petrvmakarov commented 10 years ago

workaround found: "@model object" as the first line in _Layout.cshtml

panesofglass commented 10 years ago

Interesting. Thanks for posting the work-around!