1

I am trying to scan through my site and catalog all content. To do this, I need to

  1. Find all Views (probably as Controller / Action Combinations)
  2. render all my Views to strings

To crawl my own site, I have implemented this sitemap provider. So I have an xml file like this:

<?xml version="1.0" encoding="utf-8" ?>
<mvcSiteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-3.0"
 enableLocalization="false">

  <mvcSiteMapNode title="Home" controller="Home" action="Index"
 changeFrequency="Always" updatePriority="Normal">

  <mvcSiteMapNode title="Dashboard" controller="Home" action="Dashboard"/>
  <mvcSiteMapNode title="My Profile" controller="Profile" action="MyProfile"/>
  <mvcSiteMapNode title="My Jobs" controller="Profile" action="MyJobs"/>
 </mvcSiteMapNode>
</mvcSiteMap>

that I can convert to a list using linq-to-xml.

I intended to convert each view to a string by using the RenderViewToString common approach.

Unfortunately, this works better when you want to render a particular View to a String. When you don't know the specific model/controller in advance.

In conclusion: Is there a way to render a view to string only having Controller and Action names as strings?

Community
  • 1
  • 1
Renaissance
  • 798
  • 5
  • 15
  • 2
    You could potentially iterate through your sitemap node and issue your own `System.Net.HttpClient` request to the urls (building them using the controller/action names you have). – Moo-Juice Jul 30 '13 at 18:10
  • @Moo-Juice, that's an interesting idea. I was hoping for something more elegant (EASY), but will google HttpClient to see how i can use it to render my views. – Renaissance Jul 30 '13 at 18:14
  • Probably need to query the routing so that you can iterate through all the routes. Even if you solve that: Problem will be is when your actions require parameters/models, what will you pass to them? – AaronLS Jul 30 '13 at 18:51

2 Answers2

1

There's a recent post from Rick Strahl showing how to render views outside the Asp.NET. Given the post, its seen very simple and you could write a file, or string for searching purpouse. Take a look!

Fals
  • 6,813
  • 4
  • 23
  • 43
  • this is very interesting. I'm looking closer at the original article. I'm excited to see what `ViewRenderer` in the original article does. This seems to be my answer, but he uses an `ErrorController` class that I can't find in any documentation... – Renaissance Jul 30 '13 at 19:11
  • The reference for the original article says exatly how to render Views as String. There's a link just at the introduction. Hopes this help you! – Fals Jul 30 '13 at 19:19
  • I suspect the "internal" method does NOT work for me afterall. It seems to require the model be passed. That's what I won't have for each View -- Controller / Model classes. – Renaissance Jul 30 '13 at 19:47
  • this post covers exactly what you need http://stackoverflow.com/questions/483091/render-a-view-as-a-string – Fals Jul 30 '13 at 19:50
  • yes, I found this link early today. But, unless I'm missing something, it too requires that you know the matching model for the View and pass it. – Renaissance Jul 30 '13 at 19:57
1

With All Pros and Cons, I ended up overiiding the OnResultExecuted of my base controller to capture the rendered html everytime a user visits the page.

protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
    var viewResult = filterContext.Result as ViewResult;
    var controller = filterContext.Controller;


    using (StringWriter sw = new StringWriter())
    {
        ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
        viewResult.View.Render(viewContext,sw);
        string s = sw.ToString();
    }  


    base.OnResultExecuted(filterContext);
}

The rendered HTML will be written to the db. I should probably decide on creative logic to selectively make db updates with timestamps or page update status-es.

Renaissance
  • 798
  • 5
  • 15