ServiceStack不呈现Razor View,只看到Snap Shot

时间:2015-05-21 18:19:30

标签: twitter-bootstrap razor twitter-bootstrap-3 servicestack servicestack-razor

我已经使用Bootstrap建立了一个非常基本的ServiceStack项目,并且我试图让它看到我的主页(Razor View)它没有,所以我得到了我的快照主页。以下是我创建项目的步骤:

  1. 使用Bootstrap"
  2. 创建新项目" ServiceStack ASP.Net
  3. 打开Nuget Server Console以下载错误的Servicestack软件包。
  4. 构建
  5. 将Services.cs重命名为MyStuffServices.cs,将Model.cs重命名为MyStuffModel.cs
  6. 已添加到' AppHost.cs':

    SetConfig(new HostConfig
            {
                DefaultRedirectPath = "/Home"
            }); 
    
  7. 已移动" _layout.cshtml"从/ veiws / shared到/ views文件夹

  8. 添加" Home.cshtml"到/ views
  9. 在Home.cshtml
  10. 中添加了小文字
  11. 已添加到MyStuffService.cs

    public class HomeService : Service
    {
         [DefaultView("Home")]
         public int Get(Home request)
         {
             return 0;
         }
    }   
    
  12. 已添加到MyStuffModel.cs:

    [Route("/home", "GET")]    
    public class Home : IReturn<int>
    {
    
    }
    
  13. 构建
  14. 运行到Internet Explorer - 获取快照页面。
  15. 这是我的AppHost.cs

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using Funq;
        using ServiceStack;
        using ServiceStack.Razor;
        using MyStuff.ServiceInterface;
    
        namespace MyStuff
        {
        public class AppHost : AppHostBase
        {
        /// <summary>
        /// Default constructor.
        /// Base constructor requires a name and assembly to locate web service classes. 
        /// </summary>
        public AppHost()
            : base("MyStuff", typeof(MyStuffServices).Assembly)
        {
    
        }
    
        /// <summary>
        /// Application specific configuration
        /// This method should initialize any IoC resources utilized by your web service classes.
        /// </summary>
        /// <param name="container"></param>
        public override void Configure(Container container)
        {
            //Config examples
            //this.Plugins.Add(new PostmanFeature());
            //this.Plugins.Add(new CorsFeature());
    
            this.Plugins.Add(new RazorFormat());
    
            SetConfig(new HostConfig
            {
                DefaultRedirectPath = "/Home"
            });
        }
    }
    }
    

    这是我的Global.asax

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Security;
        using System.Web.SessionState;
    
        namespace MyStuff
        {
            public class Global : System.Web.HttpApplication
        {
            protected void Application_Start(object sender, EventArgs e)
        {
            new AppHost().Init();
        }
    }
    }
    

    我没有错误,包管理员说我没有遗漏任何包裹。我觉得我有一个缺失的步骤,非常感谢任何帮助。

    更新:在启用调试时执行,这是我使用Internet Explorer打开时获得的内容

    @inherits ViewPage
    @{
        ViewBag.Title = "Hello World Service";
    }
    <div>
        <div>
            <input class="form-control input-lg" id="Name" type="text"     placeholder="Type your name">
            <p id="helloResult" style="margin-top: 15px;font-size: large"></p>
        </div>
    </div>
    <script>
        $('#Name').keyup(function () {
            var name = $('#Name').val();
            if (name) {
                $.getJSON('/hello/' + name)
                     .success(function (response) {
                         $('#helloResult').html(response.Result);
                    });
            } else {
                 $('#helloResult').html('');
            }
        });
    </script>
    

1 个答案:

答案 0 :(得分:2)

看起来这个问题可能来自现有的MVC Razor项目模板,该模板正在添加覆盖Microsoft.AspNet.Razor.3.2.2引用的NuGet包ServiceStack.Razor

避免这种情况的一种方法是在添加新视图时使用基本HTML项目模板,并使用cshtml扩展名对其进行命名。

HTML file work around

这将避免来自MVC项目模板的不需要的NuGet引用。

为了使这更直接和明显,ServiceStackVS extension has been updated包含一个简单的Razor项目模板,该模板不会添加有问题的Microsoft.AspNet.Razor.3.2.2 NuGet包,只需添加一个空白的HTML页面。

ServiceStackVS Razor item template

如果您不小心使用其中一个MVC项目模板,要修复项目,您需要执行以下步骤。

  1. 右键单击项目 - &gt;管理NuGet包
  2. 选择左上方标签中的Installed packages(VS 2013)
  3. 卸载Microsoft ASP.NET Razor相关软件包
  4. UnInstall ServiceStack.Razor
  5. ReInstall ServiceStack.Razor
  6. 在添加Razor项目之前,先前没有删除System.Web的<runtime>条目。
  7. 希望有所帮助。

相关问题