I have a custom routing system on my web application where i have a database with my routes (url
, Controller
, Action
and some more info). Every request that is made to the server goes to the database, queries that route by the url and returns a set of that i need to keep accessible for the future rendering Filter
, Controller
and View
, i keep this data at a global var at my Global.asax file:
public static class GlobalVars
{
public static Redirect reqContext { get; set; }
public static UnidadeHotSite HotSite { get; set; }
}
My problem is that right now this information is getting mixed through users, sometimes when i'm at my browser and i have 3 open tabs and i refresh them at "almost" the same time the last one will get the route data of the previous loaded one.
For example my HotSite
var keeps some subsite information like name
, url
, ID
, etc., the subsite url would be: abc.com/subsite. When i load the first tab i get the right data which is the subsite data, the second tab is off the subsite area, abc.com, and i get the same data as the last loaded tab.
Now, what could be the problem ? I already used the NoStore
on the OutputCache
and tried to disable the session but nothing seems to work.
This is my Route Handler:
[OutputCache( NoStore = true, Duration = 0 )]
public class RouteHandler : MvcRouteHandler
{
private static string RedirectAction { get; set; }
private static string UnidadeURL { get; set; }
protected override IHttpHandler GetHttpHandler( RequestContext requestContext )
{
var friendlyUrl = (string)requestContext.RouteData.Values["RedirectUrl"];
var objRet = BuscaURL( friendlyUrl, requestContext );
GlobalVars.reqContext = objRet[0] as Redirect;
return base.GetHttpHandler(objRet[1] as RequestContext);
}
private static object[] BuscaURL( string pURL, RequestContext reqContext )
{
RedirectAction = "Index";
var isHotSite = BuscaHotSiteInfo( pURL );
var tRedirect = !isHotSite ? BuscaURLWS( pURL ) : BuscaURLHS( pURL );
if( tRedirect == null || "NotFound".Equals( tRedirect.controller ) )
{
Configuracoes.GeraLog( "pURL", pURL );
if(tRedirect == null)
Configuracoes.GeraLog( "tRedirect", "NULL" );
else
HelperController.GeraLog( tRedirect );
tRedirect = RedirectController.BuscaPaginaPorUrlWS( 5 );
RedirectAction = "Index";
reqContext.RouteData.DataTokens["Namespaces"] = "Site.Controllers";
}
if( tRedirect != null && tRedirect.paginaId > 0 && RedirectAction == "Index" )
{
using( var db = new SkillSite() )
{
var pagina = db.Pagina.First( x => x.ID == tRedirect.paginaId && x.ativo == 1 );
RedirectAction = pagina.action;
}
}
reqContext.RouteData.Values["controller"] = tRedirect.controller;
reqContext.RouteData.Values["action"] = RedirectAction;
reqContext.RouteData.Values["id"] = tRedirect.ID;
return new object[] { tRedirect, reqContext };
}
private static Redirect BuscaURLHS( string pUrl )
{
Redirect redirect = null;
pUrl = pUrl.Replace( UnidadeURL, "" ).Replace( "teste", "" ).TrimStart( '/' ).TrimEnd( '/' );
if( !string.IsNullOrEmpty( pUrl ) && !string.IsNullOrWhiteSpace( pUrl ) )
{
var splitUrl = pUrl.Split( '/' ).ToList();
if( splitUrl.Count > 1 )
{
if( "cursos".Equals( splitUrl[0] ) )
{
if( splitUrl.Count == 2 )
{
redirect = RedirectController.SearchPageByUrlWS( 1, splitUrl[1] );
}
else if( splitUrl.Count == 3 )
{
redirect = RedirectController.SearchPageByUrlWS( 2, splitUrl[1], splitUrl[2] );
}
}
}
else
{
redirect = RedirectController.SearchPageByUrlWS( 0, "", "", splitUrl[0] );
}
}
else
{
redirect = RedirectController.SearchPageByUrlWS( 0, "", "", "home" );
}
return redirect;
}
private static Redirect BuscaURLWS( string pUrl )
{
Redirect redirect = null;
if( !string.IsNullOrEmpty( pUrl ) && !string.IsNullOrWhiteSpace( pUrl ) )
{
var splitUrl = pUrl.TrimEnd( '/' ).Split( '/' ).ToList();
if( splitUrl.Count > 1 )
{
if( "cursos".Equals( splitUrl[0] ) )
{
if( splitUrl.Count == 2 )
{
redirect = RedirectController.SearchPageByUrlHS( 1, splitUrl[1] );
}
else if( splitUrl.Count == 3 )
{
redirect = RedirectController.SearchPageByUrlHS( 2, splitUrl[1], splitUrl[2] );
}
}
}
else
{
redirect = RedirectController.SearchPageByUrlHS( 0, "", "", splitUrl[0] );
}
}
else
{
redirect = RedirectController.SearchPageByUrlHS( 0, "", "", "home" );
}
return redirect;
}
}
This is the Controller that makes the searchs on the DB
[OutputCache( NoStore = true, Duration = 0 )]
public class RedirectController
{
public static Redirect SearchPageByUrlWS( int tipo, string cursoCURL = "", string cursoURL = "", string redirectURL = "", string redirectURLTwo = "" )
{
using( var db = new Site() )
{
IQueryable<Redirect> redirects;
if( tipo == 1 )
{
redirects = from redirect in db.Redirect
where redirect.url == cursoCURL && redirect.cursoCatId > 0
select redirect;
}
else
{
redirects = from redirect in db.Redirect
where redirect.url == redirectURL &&
redirect.cursoCatId == 0 &&
redirect.regulamentoId == 0 &&
redirect.noticiaId == 0 &&
redirect.ebookId == 0 &&
redirect.conhecaId == 0
select redirect;
}
return (redirects.ToList().Count > 0) ? redirects.ToList()[0] : null;
}
}
public static HS_Redirect SearchPageByUrlHS( int tipo, string cursoCURL = "", string cursoURL = "", string redirectURL = "", string redirectURLTwo = "" )
{
using( var dbHS = new HS() )
{
IQueryable<HS_Redirect> redirects;
if( tipo == 4 )
{
redirects = from redirect in dbHS.HS_Redirect
where redirect.url == redirectURL && redirect.noticiaId > 0 && redirect.unidadeCE == GlobalVars.HotSite.unidadeHS.unidadeCE
select redirect;
}
else
{
redirects = from redirect in dbHS.HS_Redirect
where
redirect.url == redirectURL &&
redirect.cursoCatId == 0 &&
redirect.regulamentoId == 0 &&
redirect.noticiaId == 0 &&
redirect.ebookId == 0 &&
redirect.conhecaId == 0
select redirect;
}
return ( redirects.ToList().Count > 0 ) ? redirects.ToList()[0] : null;
}
}
}
EDIT:
I managed to make @NightOwl888 answer work with areas and everything else i needed, i'm not going to post this here since it's a little big so here is the code: http://pastebin.com/yTdWKMp4
EDIT 2
I have updated the file on pastebin with some changes to improve speed and usability: http://pastebin.com/yTdWKMp4