Getting a HttpCompileException in ServiceStack Razor view (Self hosted)

核能气质少年 提交于 2019-12-10 22:14:56

问题


ASSEMBLY 1

This is my project structure (I serve up content from embedded resources):

Common_Assembly.dll
    Css
        Common.css
    Views
        Shared
            _Layout.cshtml

This also has a class called Model.cs, which is essentially just:

public class Model
{
     public string Title {get; set; }
}

_Layout.cshtml

@model MyNameSpace.Model
<html>
    <head>
        <script language="javascript" href="css/common.css"></script>
        <title>@Model.Title</title>
    </head>
    <body>
        @RenderBody
    </body>
</html>

ASSEMBLY 2

I then have a second assembly (which references the one above) and is the one that actually hosts the web-service:

Concrete_Assembly.dll
    Views
        Index.cshtml

This has a class called IndexResponse that derives from Model in the other assembly.

public class IndexResponse : Model
{ 
}

Index.cshtml

@Model MyOtherNameSpace.IndexResponse
<p>Test</p>

Now, the problem. If I remove the @Model line, everything works correctly and I see my index page within the layout page of the other DLL (I use a custom VirtualPathProvider to locate resources across multiple Dlls). However, if I try and use the IndexResponse as a model in the index page, I get a HttpCompileException. As it is thrown by an external component I do not actually know what the exception message is (I use service stack binaries).

At first I wondered if it was because the model class was different from the layout's class (even though it derives from it). To test that theory I created a TestModel class that derives from Model (placed IN the common assembly), and used that - it worked fine.

This leads me to believe it is because the IndexResponse is in the secondary assembly - but can't be sure because I can't see the error.

Any help would be appreciated!

EDIT

For completeness, here is the actual WebService method. I do not believe anything is wrong here as it worked fine when I did my other tests (using TestModel instead).

    public IndexResponse Any(Index index)
    {
        return new IndexResponse() { Title = "Test Title" };
    }   // eo Any

EDIT 2

Apologies for all the edits. Furthermore, is there anyway I can get hold of this exception or handle it so I can have a look at the error? It would be nice to catch this and display it - otherwise developing these pages as embedded resources is going to be like pulling teeth :)

EDIT 3

After following some suggestions from Mythz, and adding the correct namespaces in to the Razornamespaces configuration, I have finally gotten hold over the error that it is throwing:

+ $exception {"(0): error CS1704: An assembly with the same simple name 'Concrete_Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null has already been imported. Try removing one of the references or sign them to enable side-by-side."} System.Exception {System.Web.HttpCompileException} `


回答1:


If you haven't already you should add any assemblies you're using to your app.config, e.g:

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, 
  Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="ServiceStack.Razor.ViewPage">
  <namespaces>
    <add namespace="ServiceStack.Html" />
    <add namespace="ServiceStack.Razor" />
    <add namespace="ServiceStack.Text" />
    <add namespace="ServiceStack.OrmLite" />
    <add namespace="Concrete_Assembly" />
    <add namespace="Common_Assembly" />
  </namespaces>
</pages>
</system.web.webPages.razor>

Also disable ASP.NET webpages:

<appSettings>
  <add key="webPages:Enabled" value="false" />
</appSettings>

See the RazorRockstars Self Host app.config for a reference.



来源:https://stackoverflow.com/questions/17544939/getting-a-httpcompileexception-in-servicestack-razor-view-self-hosted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!