ServiceStack 不呈现 Razor 视图,只看到快照 [英] ServiceStack not rendering Razor View, only seeing Snap Shot

查看:60
本文介绍了ServiceStack 不呈现 Razor 视图,只看到快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  1. 创建新项目ServiceStack ASP.Net with Bootstrap"
  2. 打开 Nuget 服务器控制台以下载缺少的 Servicestack 包.
  3. 构建.
  4. 将 Services.cs 重命名为 MyStuffServices.cs,将 Model.cs 重命名为 MyStuffModel.cs
  5. 添加到AppHost.cs":

    SetConfig(new HostConfig{DefaultRedirectPath = "/Home"});

  6. 将_layout.cshtml"从/veiws/shared 移动到/views 文件夹

  7. 在/views 中添加了Home.cshtml"
  8. 向 Home.cshtml 添加了次要文本
  9. 添加到 MyStuffService.cs

    公共类 HomeService : 服务{[默认视图(主页")]public int Get(主页请求){返回0;}}

  10. 添加到 MyStuffModel.cs:

    [Route("/home", "GET")]公共类主页:IReturn{}

  11. 构建.
  12. 运行到 Internet Explorer - 获取快照页面.

这是我的 AppHost.cs

 使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 Funq;使用服务堆栈;使用 ServiceStack.Razor;使用 MyStuff.ServiceInterface;命名空间 MyStuff{公共类 AppHost : AppHostBase{///<总结>///默认构造函数.///基本构造函数需要名称和程序集来定位 Web 服务类.///</总结>公共 AppHost(): base("MyStuff", typeof(MyStuffServices).Assembly){}///<总结>///应用特定配置///此方法应初始化您的 Web 服务类使用的任何 IoC 资源.///</总结>///<param name="container"></param>公共覆盖无效配置(容器容器){//配置示例//this.Plugins.Add(new PostmanFeature());//this.Plugins.Add(new CorsFeature());this.Plugins.Add(new RazorFormat());设置配置(新的主机配置{DefaultRedirectPath = "/Home"});}}}

这是我的 Global.asax

 使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.Security;使用 System.Web.SessionState;命名空间 MyStuff{公共类全局:System.Web.HttpApplication{protected void Application_Start(对象发送者,EventArgs e){新 AppHost().Init();}}}

我没有错误,包管理器说我没有丢失任何包.我觉得我错过了一个步骤,非常感谢任何帮助.

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

@inherits ViewPage@{ViewBag.Title = "Hello World Service";}<div><div><input class="form-control input-lg" id="Name" type="text" placeholder="输入你的名字"><p id="helloResult" style="margin-top: 15px;font-size: large"></p>

<脚本>$('#Name').keyup(function () {var name = $('#Name').val();如果(姓名){$.getJSON('/hello/' + 名字).成功(功能(响应){$('#helloResult').html(response.Result);});} 别的 {$('#helloResult').html('');}});

解决方案

看起来这个问题可能来自添加 NuGet 包的现有 MVC Razor 项模板 Microsoft.AspNet.Razor.3.2.2code> 覆盖了 ServiceStack.Razor 引用.

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

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

为了使这更直接和明显,ServiceStackVS 扩展已经更新以包含一个简单的 Razor 项目模板,该模板不会添加有问题的 Microsoft.AspNet.Razor.3.2.2 NuGet 包,而只是添加一个空白的 HTML 页面.

如果您不小心使用了 MVC 项模板之一,要修复您的项目,您需要执行以下步骤.

  1. 右键单击项目 -> 管理 NuGet 包
  2. 在左上角的选项卡 (VS 2013) 中选择 Installed packages
  3. 卸载 Microsoft ASP.NET Razor 相关软件包
  4. 卸载 ServiceStack.Razor
  5. 重新安装 ServiceStack.Razor
  6. 删除 在添加 Razor 项之前之前不存在 System.Web 的条目.

希望有所帮助.

I've set up a very basic ServiceStack project with Bootstrap and I'm trying to get it to see my homepage (Razor View) which it doesn't, so I get the Snapshot of my homepage. Here are the steps I take to create the project:

  1. Create new Project "ServiceStack ASP.Net with Bootstrap"
  2. Open Nuget Server Console to download misssing Servicestack Packages.
  3. Build.
  4. Renamed Services.cs to MyStuffServices.cs and Model.cs to MyStuffModel.cs
  5. Added to 'AppHost.cs':

    SetConfig(new HostConfig
            {
                DefaultRedirectPath = "/Home"
            }); 
    

  6. Moved "_layout.cshtml" from /veiws/shared to /views folder

  7. Added "Home.cshtml" to /views
  8. Added minor text to Home.cshtml
  9. Added to MyStuffService.cs

    public class HomeService : Service
    {
         [DefaultView("Home")]
         public int Get(Home request)
         {
             return 0;
         }
    }   
    

  10. Added to MyStuffModel.cs:

    [Route("/home", "GET")]    
    public class Home : IReturn<int>
    {
    
    }
    

  11. Build.
  12. Run to Internet Explorer - Get Snapshot page.

Here is my 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"
        });
    }
}
}

Here is my 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();
    }
}
}

I have no errors and the Package Manager says I'm not missing any packages. I feel like there is a step I'm missing, any help is greatly appreciated.

Update: When executing with debug on, this is what I get when I open with 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>

解决方案

Looks like this problem might be coming from an existing MVC Razor item template that is adding NuGet package Microsoft.AspNet.Razor.3.2.2 which is overriding the ServiceStack.Razor reference.

One way to avoid this is to use the basic HTML item template when adding a new view and name it with the cshtml extension.

This will avoid unwanted NuGet references coming from the MVC item templates.

To make this more straight forward and obvious, ServiceStackVS extension has been updated to include a simple Razor item template that doesn't add the problematic Microsoft.AspNet.Razor.3.2.2 NuGet packages and simply adds a blank HTML page.

If you do accidentally use one of the MVC item templates, to fix your project you will want to do the following steps.

  1. Right click the project -> Manage NuGet Packages
  2. Select Installed packages at the top left tab (VS 2013)
  3. Uninstall Microsoft ASP.NET Razor related packages
  4. UnInstall ServiceStack.Razor
  5. ReInstall ServiceStack.Razor
  6. Delete <runtime> entries for System.Web weren't previously present before adding the Razor item.

Hope that helps.

这篇关于ServiceStack 不呈现 Razor 视图,只看到快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆