使用ServiceStack / Razor时,我只在Azure中呈现视图时出现问题。
http://nehcr-dev.azurewebsites.net/with-view
以下是本地页面的屏幕截图:
!https://www.dropbox.com/s/up5bmixdjmcc9bi/with-view.png?dl=0
以下是我的服务回复:
return new HttpResult(new IndexResponse("Home | Northeast Healthcare Recruitment")
{
TotalJobs = metadata.TotalJobs,
GroupedJobTypeCounts = groupedJobTypeCounts,
MostRecentJobs = new List<JobListingSummary>()
})
{
View = "Index"
};
其中IndexResponse是
public class IndexResponse : BaseResponse
{
public IndexResponse(string title) : base(title) { }
public int TotalJobs { get; set; }
public List<List<JobTypeCount>> GroupedJobTypeCounts { get; set; }
public List<JobListingSummary> MostRecentJobs { get; set; }
}
和BaseResponse是
public abstract class BaseResponse
{
public BaseResponse(string title)
{
if (string.IsNullOrWhiteSpace(title))
{
Title = "Northeast Healthcare Recruitment";
}
Title = title;
}
public string Title { get; set; }
}
我的观点的开头看起来像:
@inherits ViewPage<NEHCR.ServiceModel.IndexResponse>
@section head{
}
<section id="content" class="nehcr-content">
<!-- JOB SEARCH START -->
<section class="nehcr-search-hero">
<div class="container">
<div>
<div class="nehcr-search-text">
<h1 class="nehcr-search-text-header">Search from @Model.TotalJobs job offers</h1>
<p class="nehcr-search-text-p">Your career starts here.</p>
</div>
<form method="GET" action="/jobs" class="nehcr-search-wrap nehcr-row">
我的web.config是:
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false"/>
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false"/>
</sectionGroup>
</configSections>
<appSettings>
<add key="webPages:Enabled" value="false" />
<add key="ConnectionString" value="" />
</appSettings>
<connectionStrings>
<add name="ConnectionString" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>
<!--
For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.
The following attributes can be set on the <httpRuntime> tag.
<system.Web>
<httpRuntime targetFramework="4.6.2" />
</system.Web>
-->
<system.web>
<compilation debug="true" targetFramework="4.6.2">
<buildProviders>
<add extension=".cshtml" type="ServiceStack.Razor.CSharpRazorBuildProvider, ServiceStack.Razor"/>
</buildProviders>
</compilation>
<httpRuntime targetFramework="4.5"/>
<httpHandlers>
<add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*"/>
</httpHandlers>
<customErrors mode="Off">
</customErrors>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<urlCompression doStaticCompression="true" doDynamicCompression="false"/>
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true"/>
</handlers>
</system.webServer>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<pages pageBaseType="ServiceStack.Razor.ViewPage">
<namespaces>
<add namespace="System"/>
<add namespace="System.Linq"/>
<add namespace="ServiceStack"/>
<add namespace="ServiceStack.Html"/>
<add namespace="ServiceStack.Razor"/>
<add namespace="ServiceStack.Text"/>
<add namespace="ServiceStack.OrmLite"/>
<add namespace="NEHCR"/>
<add namespace="NEHCR.ServiceModel"/>
</namespaces>
</pages>
</system.web.webPages.razor>
本地运行正常,Win10上的IISExpress。部署到Azure我得到了这个:
System.NullReferenceException occurred
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=nmhpfpfd
StackTrace:
at ASP.Views.Home.__Index.Execute() in d:\home\site\wwwroot\Views\Home\Index.cshtml:line 15
at ServiceStack.Razor.ViewPage`1.WriteTo(StreamWriter writer)
at ServiceStack.Razor.Managers.RazorPageResolver.ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, Object model, IRazorView pageInstance, Func`1 layout)
at ServiceStack.Razor.Managers.RazorPageResolver.ExecuteRazorPage(IRequest httpReq, IResponse httpRes, Object model, RazorPage razorPage)
at ServiceStack.Razor.Managers.RazorPageResolver.ProcessRequest(IRequest httpReq, IResponse httpRes, Object dto)
at ServiceStack.Formats.HtmlFormat.<>c__DisplayClass10_0.<SerializeToStream>b__0(IViewEngine x)
at ServiceStack.Formats.HtmlFormat.SerializeToStream(IRequest req, Object response, IResponse res)
ServiceStack.Razor.ViewPageBase<TModel>.Model.get returned null.
如果我将服务响应更改为:
return new IndexResponse("Home | Northeast Healthcare Recruitment")
{
TotalJobs = metadata.TotalJobs,
GroupedJobTypeCounts = groupedJobTypeCounts,
MostRecentJobs = new List<JobListingSummary>()
};
效果很好:http://nehcr-dev.azurewebsites.net/
感谢您的任何见解。