Search code examples
c#wcf

Handling the returned html in a wcf service


I have to edit a wcf service hosted under IIS.

Looks like a normal web service, I can add and remove [OperationContract] methods under the public project interface and having them updated into the SOAP envelope. Brilliant. Issues comes here: I have to refactor all the frontend stuff (and a huge part of the backend) and I was wishing to add a templating engine to the project, cause the web side stuff is super redundant and bad written at the moment. I've opted for DotLiquid library, but if you guys have better solutions feel free to suggest stuff, i'm new to .NET environment. That said, i'm seeing that all the html files are located under a web dir under the root. I can get them by browsing at ip_addr:port/web/fie.html, but i can't get where that's handled in the project. I would like to write custom "paths" to handle specific views, reading html files from disk, rendering them, and, finally, serving them as a text/html response. I read some stuff around, but all of those deals about serving html slices as response, i want to handle pages templating with a path-view logic, a bit different.

For example, under Python, using Django framework, you are able to define paths that drives to specific views which can read and render files, serving them as HtmlHttpResponse.
Is it possible to accomplish that with a wcf service?

Thanks in advance.
Hele.


Solution

  • I solved by including the package Microsoft.AspNet.Mvc (https://www.asp.net/mvc).
    By doing so, project is able to use a Model-view-controller logic.

    Microsoft.AspNet.Mvc allows developers to write controllers that use ActionResults to handle requests. This packet comes with Razor, which permits to render and return dynamic pages throught the service.
    Of course, working with an aspnet mvc project would be way better. I'm sharing all of this because developers may be forced to work with old wcf services which may benefit on implementing mvc logics.
    I'll leave an example on how to set the base stuffs.

    Tree

    └── Project(Service)/
        ├── App_Data
        ├── App_Start/
        │   └── RouteConfig.cs
        ├── Controllers/
        │   └── HomeController.cs
        ├── Views/
        │   ├── Home/
        │   │   └── Index.cshtml
        │   ├── Shared/
        │   │   └── _Layout.cshtml
        │   ├── _ViewStart.cshtml
        │   └── Web.config (Auto generated)
        └── Global.asax
    

    Base controller (HomeController.cs)

    using System.Web.Mvc;
    
    namespace Service.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                // C# Logic goes here
                return View();
            }
        }
    }
    

    Routes (RouteConfig.cs)

    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace Service
    {
        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.IgnoreRoute("{resource}.svc/{*pathInfo}");
    
    
                // Default
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}",
                    defaults: new { controller = "Home", action = "Index" }
                );
            }
        }
    }
    

    Global asax (Global.asax)

    using System;
    using System.Web.Routing;
    
    namespace Service
    {
        public class Global : System.Web.HttpApplication
        {
    
            protected void Application_Start(object sender, EventArgs e)
            {
                RouteConfig.RegisterRoutes(RouteTable.Routes);
            }
    
            protected void Session_Start(object sender, EventArgs e) {}
    
            protected void Application_BeginRequest(object sender, EventArgs e) {}
    
            protected void Application_AuthenticateRequest(object sender, EventArgs e) {}
    
            protected void Application_Error(object sender, EventArgs e) {}
    
            protected void Session_End(object sender, EventArgs e) {}
    
            protected void Application_End(object sender, EventArgs e) {}
        }
    }
    

    Shared html (_Layout.cshtml)

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Service</title>
    </head>
    <body>
        <header></header>
        <main>@RenderBody()</main>
        <footer></footer>
    </body>
    </html>
    

    Starting view (_ViewStart.cshtml)

    @{ Layout = "~/Views/Shared/_Layout.cshtml"; }
    

    Base view (Index.cshtml)

    <!-- MAIN -->
    <p>MAIN</p>
    

    Loading the Home/Index route will return the assembled page as expected.

    enter image description here

    hope it helps.
    Hele.