4

I have an app with links like so in default.aspx...

<link href="Styles/smoothness/jquery-ui-1.8.14.custom.css" rel="stylesheet" type="text/css" />
<script src="Scripts/json2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-ui-1.8.14.custom.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.tmpl.min.js" type="text/javascript"></script>  

Once I added URL Routing it broke my relative paths. So if I get deep into a route then the page can't find images, scripts, etc. I would imagine that my jQuery service calls are probably broken as well. Is there a way other than adding "/" to the beginning of every relative reference to fix this. In my global.asax I currently have...

void RegisterRoutes(RouteCollection routes)
{

    routes.MapPageRoute(
        "EntityRoute", 
        "{entityID}", 
        "~/Default.aspx");        

    routes.MapPageRoute(
        "GlobalSearchRoute",
        "{entityID}/{guarantorID}/{guarDOB}",
        "~/Default.aspx");
}

Can I add something here that would allow my relative paths to function without changing all of those paths in the site?

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
CtrlShiftF11
  • 172
  • 2
  • 10

2 Answers2

1

You can use Routes.IgnoreRoute in global.asax to exclude those routes that reference your static content:

routes.IgnoreRoute("Styles/{*pathInfo}");
routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("Images/{*pathInfo}");

etc.

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Yes, apparently, but the notation is slightly different: http://stackoverflow.com/questions/273447/how-to-ignore-route-in-asp-net-forms-url-routing – McGarnagle Apr 25 '12 at 00:27
  • 1
    It occurred to me that this is a client side problem by the time this happens so the only way to fix this would be in the code itself. I would either have to move away from the relative reference of "Images/" to "/Images/" or I would have to wrap serverside code around those links and preprocess them before they exploded in the browser - like with <%= Url.Content( or something like that. I ended up just fixing the problem by using the absolute reference "/". Thanks for trying to help me though. – CtrlShiftF11 Apr 25 '12 at 01:23
  • routes.IgnoreRoute is a method of Class RouteCollectionExtensions inside System.Web.Mvc Namespace use routes.Ignore for WebForms – Ahm3d Said Mar 03 '14 at 09:40
  • I wish we could find a proper solutoin to this. My issue is that my websites are setup in to virtual directories. So I'll have to add "/vir-dir-name", and I really don't want to do that.. :-/ – Krishnan Dec 08 '15 at 20:34
1

You can simply use the html base tag.

In the aspx:

<base id="baseControl" runat="server" />

In the code behind:

protected void Page_Init(object sender, EventArgs e)
{
    baseControl.Attributes.Add("href", Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/"));
}

Hope this helps.

Diego Plutino
  • 83
  • 1
  • 9