0

I am currently developing a website that allows users to customise various aspects of it.

I have been able to upload, retrieve and cache custom css rules, and also images, however strings are eluding me. I am using ASP.net MVC5.

Here is the code I have so far:

FileController:

[HttpGet]
[OutputCache(Duration = 3600, Location = OutputCacheLocation.Server)]
public ContentResult getString()
{
   string randString = getStringFromDatabase(); //retrieves hard coded string for now
   if (randString != null && randString.Length > 0)
      return Content(randString);
   else
      return null;
}

Then with this controller, I am planning to use @Url.Action to call the above method in FileController and retrieve the string. I have previously been able to retrieve images (since you put url action in the src attribute of the img tag) and also load custom CSS file (since you put url action in the src attribute of the link tag) both of these are cacheable with similar code to the above.

I achieve this with images like this:

<img class="navbar-logo" src="@Url.Action( "image", "file", new { name = "Logo.png" })" />

and would like to do this with strings.

I did attempt using a jQuery $.get function, but then it doesnt cache the string and loads the string after every page load.

If it helps, I am trying to save a custom page title in the database, then retrieve it on first load, and cache it.

TL;DR: I have a string stored in a database. I want to retrieve this string through a razor action (Url.Action or others) and show it to the user of the webpage. I also want to cache it. I can do it with images and css files because <img> and <link> tags both have src attributes, but <p>/<span> and other text elements do not.

If you need any more information, just let me know!

tereško
  • 58,060
  • 25
  • 98
  • 150
wh-dev
  • 537
  • 5
  • 18
  • Could you clarify exactly which part isn't doing what you expect? –  May 23 '14 at 08:32
  • @DaveParsons I added a tl;dr. It's not that it's not doing what I expected, I just dont know how to achieve the result I want :) – wh-dev May 23 '14 at 08:42
  • As far as I know what you are doing should work - the use of the data from the request shouldn't have an impact on the output cache; as it says it simply caches the output of the response. –  May 23 '14 at 08:58
  • But how do I get the string with @Url.Action and display it in the view? AFAIK there are no text-based html elements that allow a src attribute. – wh-dev May 23 '14 at 09:13
  • 1
    Use one of Html.RenderAction or Html.Action... more info in [this answer](http://stackoverflow.com/a/2955273/61470) –  May 23 '14 at 09:18
  • Thank you dave, I was able to find an answer with the link you provided - ill make an answer to go through it for anyone else who might need to do the same thing. – wh-dev May 23 '14 at 09:43

1 Answers1

0

So I figured it out with a link provided by DaveParsons (see comments under my original question.)

Essentially, I didnt need to use @Url.Action, the answer was @Html.Action:

<h4 id="page-title">@Html.Action("GetTitle", "file", new { type = "pagetitle" })</h4>

But this was returning an error, saying you cant set the outputcache location with a child action, as it only supports the attributes 'Duration, VaryByCustom and VaryByParam'. I tried it without the location, but trying to then flush the cache wasn't working, so I removed the attribute entirely and it now caches the title, and also is removed on updating it.

So I had to create a new block of code inside the controller:

    [HttpGet]
    public ContentResult GetTitle(string type)
    {
        string titleString = getStringFromByte(this.DataProvider.CustomizationFileManager.LoadCustomizationFile("WebsitePageTitle.txt"));
        if (titleString != null && titleString.Length > 0)
            return Content(titleString);
        else
            return Content("");
    }

My answer seems a bit convoluted (haven't had coffee yet). So I'll provide a...

TL;DR: Use @Html.Action. Remove [OutputCache] attribute from controller method.

wh-dev
  • 537
  • 5
  • 18