ServiceStack not rendering Razor View, only seeing

2019-07-20 22:43发布

问题:

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>

回答1:

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.