2

I'm trying to use donut-caching on the Site.Master page for things like the User Login and Shopping Cart, so that we can put OutputCache on some of the more resource intensive pages in our app.

Currently, I'm using the tag and then writing out the html from the static method in the code behind.

      <asp:Substitution ID="Substitution1" runat="server" MethodName="RenderUserLogin"/>

    public static string RenderUserLogin( HttpContext incomingContext )
    {

        System.Text.StringBuilder osb = new System.Text.StringBuilder();

        osb.Append( "<p>" );
        if ( incomingContext.User.Identity.IsAuthenticated 
            && !string.IsNullOrEmpty( incomingContext.User.Identity.Name ) )
        {
            osb.Append( "Hi, <span class=\"name\">" );
            osb.Append( "<a href=\"/Users/Show\">" );

           // ... etc. ...

       return osb.ToString();

    }

I'd rather have the html code in a partial view (.ascx) and render that out to a string, because I really don't like having specific html elements that are used by javascript buried in compiled code.

I've looked at this post which looks like it's a valid idea and could be modified to render the partial to a string: Render Partial through Controller

Are there any cleaner ways to render a partial to a string without having to go through a controller?

Community
  • 1
  • 1
marcel_g
  • 1,980
  • 2
  • 17
  • 19
  • Why do you want to avoid going through a controller? – Cymen Sep 09 '11 at 14:46
  • Thanks for the reply Cymen, but I don't really remember what we were trying to do at the time. We were using MVC Preview 3 or some early release, and now that we're onto MVC 2.0, it might be a moot question. Rendering a partial through a controller action seems like the logical solution now, and it's actually doing that now. We're not using output caching, but now we have js AJAX calling controller/action/renderpartial for the parts of site.master that we wouldn't want to have cached. It may just have been one of those things that needed to get dropped in order to launch on time. :-/ – marcel_g Sep 14 '11 at 14:06
  • Ah! Sorry, I missed the date on the question. I'm avoiding output caching for now too except in one test case. Hopefully MVC 4 will make caching more sensible/easy. – Cymen Sep 14 '11 at 15:10

0 Answers0