Answered by:
why not allow CacheProfile attribute into String method under Controller class

Question
-
User1772251356 posted
Hello,
After implemnet Cache attrubete under Web.Config and use the CachProfile attribut for String Method when run application i will get run time error ,
We can use only few attributes like Durection, varyByParam etc....
why i can't use this CacheProfiles attribute and how to fix the error.
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="Cache1Min" duration="60" varyByParam="none"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>[OutputCache(CacheProfile = "Cache1Min")]
[ [ChildActionOnly]]
public string EmployeeCount()
{
List<Employee> Emp = db.Employees.Where(e => e.IsActive == true).ToList();
return Emp.Count().ToString();
}thank you.
Friday, June 28, 2019 1:02 PM
Answers
-
User1520731567 posted
Hi santosh1991,
It is a bug.
Refering to the official article from MSDN,it does also appear the format of '[OutputCache(CacheProfile...'.
But looking at source, they definitely don't support any attribute other than Duration and VaryByParam.
Here is the main piece of code that smells:
if (Duration <= 0) { throw new InvalidOperationException(MvcResources.OutputCacheAttribute_InvalidDuration); } if (String.IsNullOrWhiteSpace(VaryByParam)) { throw new InvalidOperationException(MvcResources.OutputCacheAttribute_InvalidVaryByParam); } if (!String.IsNullOrWhiteSpace(CacheProfile) || !String.IsNullOrWhiteSpace(SqlDependency) || !String.IsNullOrWhiteSpace(VaryByContentEncoding) || !String.IsNullOrWhiteSpace(VaryByHeader) || _locationWasSet || _noStoreWasSet) { throw new InvalidOperationException(MvcResources.OutputCacheAttribute_ChildAction_UnsupportedSetting); }
Someone has already submitted a bug, but until now there is still no better solution, but the error message will be more friendly:
OutputCacheAttribute for child actions only supports Duration, VaryByCustom, and VaryByParam values. Please do not set CacheProfile, Location, NoStore, SqlDependency, VaryByContentEncoding, or VaryByHeader values for child actions.
There are also some solutions, such as implementing your own OutputCache to read WebConfig, but I don't know what the side effects are:
public class ChildActionOutputCacheAttribute : OutputCacheAttribute { public ChildActionOutputCacheAttribute(string cacheProfile) { var settings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection("system.web/caching/outputCacheSettings"); var profile = settings.OutputCacheProfiles[cacheProfile]; Duration = profile.Duration; VaryByParam = profile.VaryByParam; VaryByCustom = profile.VaryByCustom; } }
[ChildActionOnly] //[OutputCache(CacheProfile = "Cache1Min")] [ChildActionOutputCache("Cache1Min")] public string EmployeeCount() { .... }
More details,you could refer to:
https://stackoverflow.com/questions/4728958/caching-childactions-using-cache-profiles-wont-work
Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 1, 2019 8:30 AM
All replies
-
User753101303 posted
Hi,
Always tell which error you have so that we can move directly to helping on the problem you see rather than having first to guess which bad things happens (sometimes trying is not even enough to get the same error).
My guess is that you have restrictions on caching as this is a child action (for example you can't cache that alone on the browser side). Isn't it what the error message is trying to warn about?
Friday, June 28, 2019 1:19 PM -
User1772251356 posted
I got that error below I attached
Friday, June 28, 2019 1:33 PM -
User-474980206 posted
Because MVC has never supported it. While this feature has been requested, MVC is at end of life, so don't expect it to be added.
note: I have always though child actions were a poor design and should be avoided. asp.net core's view components are a much better solution.
Friday, June 28, 2019 3:21 PM -
User1520731567 posted
Hi santosh1991,
It is a bug.
Refering to the official article from MSDN,it does also appear the format of '[OutputCache(CacheProfile...'.
But looking at source, they definitely don't support any attribute other than Duration and VaryByParam.
Here is the main piece of code that smells:
if (Duration <= 0) { throw new InvalidOperationException(MvcResources.OutputCacheAttribute_InvalidDuration); } if (String.IsNullOrWhiteSpace(VaryByParam)) { throw new InvalidOperationException(MvcResources.OutputCacheAttribute_InvalidVaryByParam); } if (!String.IsNullOrWhiteSpace(CacheProfile) || !String.IsNullOrWhiteSpace(SqlDependency) || !String.IsNullOrWhiteSpace(VaryByContentEncoding) || !String.IsNullOrWhiteSpace(VaryByHeader) || _locationWasSet || _noStoreWasSet) { throw new InvalidOperationException(MvcResources.OutputCacheAttribute_ChildAction_UnsupportedSetting); }
Someone has already submitted a bug, but until now there is still no better solution, but the error message will be more friendly:
OutputCacheAttribute for child actions only supports Duration, VaryByCustom, and VaryByParam values. Please do not set CacheProfile, Location, NoStore, SqlDependency, VaryByContentEncoding, or VaryByHeader values for child actions.
There are also some solutions, such as implementing your own OutputCache to read WebConfig, but I don't know what the side effects are:
public class ChildActionOutputCacheAttribute : OutputCacheAttribute { public ChildActionOutputCacheAttribute(string cacheProfile) { var settings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection("system.web/caching/outputCacheSettings"); var profile = settings.OutputCacheProfiles[cacheProfile]; Duration = profile.Duration; VaryByParam = profile.VaryByParam; VaryByCustom = profile.VaryByCustom; } }
[ChildActionOnly] //[OutputCache(CacheProfile = "Cache1Min")] [ChildActionOutputCache("Cache1Min")] public string EmployeeCount() { .... }
More details,you could refer to:
https://stackoverflow.com/questions/4728958/caching-childactions-using-cache-profiles-wont-work
Best Regards.
Yuki Tao
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 1, 2019 8:30 AM -
User753101303 posted
My guess is that if caching profiles were allowed for child actions were allowed it would be quite easy to change a profile and possibly break your app. Child actions are restrictive because they are part of the same http request than their parent action and so some caching options are discutable or just won't make any sense.
Monday, July 1, 2019 8:38 AM