There is no trailing slash in the host name, HttpResponse.RemoveOutputCacheItem does not work

Here I am a little pickle.

I have an action for which the output is pretty static until another action is used to update the data source for the first action. I am using HttpResponse.RemoveOutputCacheItem to remove this cached output so that it is updated the next time the user loads it.

Basically I have an action like this:

[OutputCache(Duration=86400, Location=OutputCacheLocation.Server)] public ActionResult Index() { return ... } 

on my HomeController and another action on another controller that updates the information used in the first:

 public ActionResult SaveMenu(int id, Menu menu) { ... HttpResponse.RemoveOutputCacheItem(Url.Action("Index", "Home")); ... } 

The crazy thing is that this works if you download the URLs http://site/ or http://site/Home/Index . When you use the URL http://site , it is never updated.

Why is this?

+4
source share
3 answers

This is due to how OutputCacheAttribute works, namely, its dependency on a RouteData -null RouteData . Relevant Part:

 public override void OnResultExecuting(ResultExecutingContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if (!filterContext.IsChildAction) { new OutputCachedPage(this._cacheSettings).ProcessRequest(HttpContext.Current); } } 

ResultExecutingContext filterContext derived from ControllerContext . This is the source for ControllerContext.IsChildAction :

 public virtual bool IsChildAction { get { RouteData routeData = this.RouteData; if (routeData == null) { return false; } return routeData.DataTokens.ContainsKey("ParentActionViewContext"); } } 

So why does this apply to your question?

Because when you omit the "/", your Route doesn't match anything. The default route is " / ". An article explaining this in more detail is given here: http://www.58bits.com/blog/2008/09/29/ASPNet-MVC-And-Routing-Defaultaspx.aspx . This was written to explain why the Default.aspx needed in ASP.NET MVC 1 projects, but the reason lies in the same place.

So basically, RouteData is null, so OutputCacheAttribute cannot work. You can solve your problem by doing what Michael Jasper suggested and using URL rewriting.

+5
source

IIS has a very useful module called URL Rewrite. One option is to remove or add a trailing slash to all / specific URLs. If this is just the end slash that is the problem, this should work.

+1
source

I have seen similar behavior in how SharePoint behaves. SharePoint got confused with http://site ; he could not determine if the URL was a File or a SharePoint Site . Probably something similar is happening here.

You probably solved the problem by adding a URL with a trailing slash; but just in case you do not have:

 url = string.Format( "{0}/", url.TrimEnd( '/' ) ); 
0
source

Source: https://habr.com/ru/post/1340441/


All Articles